【问题标题】:Catch both links and ips with python3使用 python3 捕获链接和 ips
【发布时间】:2016-07-18 22:00:08
【问题描述】:

在论坛的帮助下,我制作了一个脚本,可以捕捉到本页主题的所有链接https://www.inforge.net/xi/forums/liste-proxy.1118/。这些主题包含代理列表。脚本是这样的:

import urllib.request, re
from bs4 import BeautifulSoup

url = "https://www.inforge.net/xi/forums/liste-proxy.1118/"
soup = BeautifulSoup(urllib.request.urlopen(url), "lxml")

base = "https://www.inforge.net/xi/"

for tag in soup.find_all("a", {"class":"PreviewTooltip"}):
    links = tag.get("href")
    final = [base + links]

final2 = urllib.request.urlopen(final)

for line in final2:
    ip = re.findall("(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3}):(?:[\d]{1,5})", line)
    ip = ip[3:-1]

for addr in ip:
    print(addr)

输出是:

Traceback (most recent call last):
  File "proxygen5.0.py", line 13, in <module>
    sourcecode = urllib.request.urlopen(final)
  File "/usr/lib/python3.5/urllib/request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 456, in open
    req.timeout = timeout
AttributeError: 'list' object has no attribute 'timeout'

我知道问题出在:final2 = urllib.request.urlopen(final) 但我不知道如何解决

如何打印ips?

【问题讨论】:

  • 问题是final = [base + links] 创建了一个包含一个元素的列表,而不是使用final2 = urllib.request.urlopen(final) 的列表,您应该传递一个字符串(网址)而不是列表。
  • 是的..我很愚蠢,你是对的..那我怎么能绕过这个呢?如果你有空回答
  • final = [base + links] 替换为final = base + links。请注意,您只保留从标签中解析的最后一个 final,如果您需要所有这些,则应更改其他代码。
  • 我是一个新手,我正在继续尝试,但我仍然没有意识到我应该在“其他代码”中更改什么,就像你首先说的那样。大声笑

标签: python python-3.x hyperlink timeout try-catch


【解决方案1】:

这段代码应该做你想做的,它被注释了,所以你可以理解所有的段落:

import urllib.request, re
from bs4 import BeautifulSoup

url = "https://www.inforge.net/xi/forums/liste-proxy.1118/"
soup = BeautifulSoup(urllib.request.urlopen(url), "lxml")

base = "https://www.inforge.net/xi/"

# Iterate over all the <a> tags
for tag in soup.find_all("a", {"class":"PreviewTooltip"}):
    # Get the link form the tag
    link = tag.get("href")
    # Compose the new link
    final = base + link

    print('Request to {}'.format(final))    # To know what we are doing
    # Download the 'final' link content
    result = urllib.request.urlopen(final)

    # For every line in the downloaded content
    for line in result:
        # Find one or more IP(s), here we need to convert lines to string because `bytes` objects are given
        ip = re.findall("(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3}):(?:[\d]{1,5})", str(line))
        # If one ore more IP(s) are found
        if ip:
            # Print them on separate line
            print('\n'.join(ip))

【讨论】:

  • 你是天使!你帮助了像我这样的新手。我不知道该怎么感谢你……你是最棒的
  • 最后一个问题,然后我已经完成了..如果我将所有捕获的 ips 保存在一个文件中,我该怎么办?我试过了:out_file = open("proxy.txt","w") out_file.write(ip) out_file.close() 但它只保存了一个 ip。
  • 您需要以append模式打开文件,否则所有内容都被覆盖,为此,请在打开文件时使用"a",而不是"w"
  • 如果你想学习,我的建议是看看这本免费的书diveintopython3.net;)
  • 再次感谢。无论如何,我把所有关于 html.it 的指南都红了,但我还是有点卡住了.. 但我正在学习! :D
猜你喜欢
  • 1970-01-01
  • 2021-01-23
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多