【问题标题】:ı am trying to get proxyies and ports ı get the proxyses but ı cant get the ports plese help me我正在尝试获取代理和端口我获取代理但我无法获取端口请帮助我
【发布时间】:2020-12-26 05:54:59
【问题描述】:
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)

【问题讨论】:

  • 请确保您的标题总结了具体问题,例如“如何使用没有类的 BeautySoup 提取标签”,然后总结问题究竟是什么“无法获取端口号”。如果您可以添加一些废弃的数据结构,以便有人可以帮助您而无需实际运行您的代码,这也会很有帮助。请检查此链接以提高问题质量,以便更多人可以帮助您stackoverflow.com/help/how-to-ask

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


【解决方案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)

【讨论】:

  • 原则上回答您自己的问题是可以的,但您在这里所做的只是复制问题中的相同代码。
【解决方案2】:

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

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

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

类似于&lt;td&gt;4145&lt;/td&gt;。清理这里的html标签或从td中提取字符串,你应该得到端口号。

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

【讨论】:

  • Traceback(最近一次调用最后):文件“C:/Users/tedad/PycharmProjects/keylogger/denemee.py”,第 16 行,在 full = i.find_next_siblings()[0 ] AttributeError: 'str' 对象没有属性 'find_next_siblings' 进程以退出代码 1 结束
  • 请在a = soup.findAll(scope="row")之后写下这个循环。从下一步开始,您将清除 a 标签,它给出了错误。
  • 我可以发送我的任何桌面代码请帮助我尝试 38 小时
猜你喜欢
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多