我正在尝试获取代理服务器和端口我获取了代理服务器但是我无法获取端口plese帮助我

时间:2020-09-07 20:30:36

标签: python python-3.x python-2.7 beautifulsoup python-requests

import requests
from bs4 import BeautifulSoup as bs

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"}
url = "https://www.proxyscan.io/"

r=requests.get(url,headers=headers)

soup = bs(r.content,"html.parser")
a = soup.findAll(scope="row")
a = str(a).replace("<th scope=\"row\">", "").replace("</th>", "").replace("[","").replace("]","").replace(" ","")
a = a.split(",")


for proxy in a:
    print(proxy)

2 个答案:

答案 0 :(得分:1)

import requests
from bs4 import BeautifulSoup as bs
    
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"}

url = "https://www.proxyscan.io/"
    
r=requests.get(url,headers=headers)
    
soup = bs(r.content,"html.parser")

a = soup.findAll(scope="row")

a = str(a).replace("<th scope=\"row\">", "").replace("</th>", "").replace("[","").replace("]","").replace(" ","")

a = a.split(",")
    
    
for proxy in a: 
   print(proxy)

答案 1 :(得分:0)

您可以使用find_next_siblings()函数来获取下一个可用标签。

因此,通过敏锐地观察已解析的html,我们可以看到该端口是代理之后的下一个标记。因此,我们可以遍历变量a并找到下一个相邻标签。

find_next_siblings()返回的数组中获取第一个元素。

这将是这样的<td>4145</td>。从中清除html标签或从td中提取字符串,您应该获取端口号。

for i in a:
    full = i.find_next_siblings()[0]
    port = str(full).replace("<td>","")
    port = str(port).replace("</td>", "")
    print(port)