Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python3中隔离HTML页面的一部分_Html_Python 3.x_Text - Fatal编程技术网

如何在Python3中隔离HTML页面的一部分

如何在Python3中隔离HTML页面的一部分,html,python-3.x,text,Html,Python 3.x,Text,我制作了一个简单的脚本来检索页面的源代码,但我想“隔离”ips的部分,以便保存到proxy.txt文件。有什么建议吗 import urllib.request sourcecode = urllib.request.urlopen("https://www.inforge.net/xi/threads/dichvusocks-us-15h10-pm-update-24-24-good-socks.455588/") sourcecode = str(sourcecode.read()) ou

我制作了一个简单的脚本来检索页面的源代码,但我想“隔离”ips的部分,以便保存到proxy.txt文件。有什么建议吗

import urllib.request

sourcecode = urllib.request.urlopen("https://www.inforge.net/xi/threads/dichvusocks-us-15h10-pm-update-24-24-good-socks.455588/")
sourcecode = str(sourcecode.read())
out_file = open("proxy.txt","w")
out_file.write(sourcecode)
out_file.close()

为什么不使用re
我需要源代码来说明具体操作方法。

我在代码中添加了几行,唯一的问题是UI版本(检查页面源代码)被添加为IP地址

import urllib.request
import re

sourcecode = urllib.request.urlopen("https://www.inforge.net/xi/threads/dichvusocks-us-15h10-pm-update-24-24-good-socks.455588/")
sourcecode = str(sourcecode.read())
out_file = open("proxy.txt","w")
out_file.write(sourcecode)
out_file.close()

with open('proxy.txt') as fp:
    for line in fp:
        ip = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line)

for addr in ip:
    print(addr)
更新: 这就是您所寻找的,BeatifulSoup只能使用CSS类从页面中提取我们需要的数据,但是它需要使用pip安装。您不需要将页面保存到文件中

from bs4 import BeautifulSoup
import urllib.request
import re

url = urllib.request.urlopen('https://www.inforge.net/xi/threads/dichvusocks-us-15h10-pm-update-24-24-good-socks.455588/').read()
soup = BeautifulSoup(url, "html.parser")

# Searching the CSS class name
msg_content = soup.find_all("div", class_="messageContent")

ips = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', str(msg_content))

for addr in ips:
    print(addr)

谢谢!这是一个起点!但是,也许可以只关注html页面的一部分(在本例中),这样脚本就可以只打印IP?无论如何,谢谢你,我很蠢。。“ip”是一个列表,所以我可以删除其中的项目。