【问题标题】:Using urlopen to open list of urls使用 urlopen 打开 url 列表
【发布时间】:2014-06-10 07:51:31
【问题描述】:

我有一个 python 脚本,它可以获取网页并对其进行镜像。它适用于一个特定页面,但我无法让它适用于多个页面。我以为我可以将多个 URL 放入一个列表中,然后将其提供给函数,但我得到了这个错误:

Traceback (most recent call last):
  File "autowget.py", line 46, in <module>
    getUrl()
  File "autowget.py", line 43, in getUrl
    response = urllib.request.urlopen(url)
  File "/usr/lib/python3.2/urllib/request.py", line 139, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.2/urllib/request.py", line 361, in open
    req.timeout = timeout
AttributeError: 'tuple' object has no attribute 'timeout'

这是有问题的代码:

url = ['https://www.example.org/', 'https://www.foo.com/', 'http://bar.com']
def getUrl(*url):
    response = urllib.request.urlopen(url)
    with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
        shutil.copyfileobj(response, out_file)
getUrl()

我已经用尽了谷歌试图找到如何使用 urlopen() 打开列表的方法。我找到了一种可行的方法。它需要一个 .txt 文档并逐行遍历它,将每一行作为 URL 提供,但我正在使用 Python 3 编写它,无论出于何种原因 twillcommandloop 都不会导入。另外,这种方法很笨拙,并且需要(据说)不必要的工作。

无论如何,任何帮助将不胜感激。

【问题讨论】:

  • 你为什么不简单地用for循环遍历你的URL列表?
  • 回复sheng的评论时突然想到这个!它会将特定部分作为字符串返回,对吗?

标签: python urllib urlopen


【解决方案1】:

您应该使用for 循环遍历您的网址:

import shutil
import urllib.request


urls = ['https://www.example.org/', 'https://www.foo.com/']

file_name = 'foo.txt'

def fetch_urls(urls):
    for i, url in enumerate(urls):
        file_name = "page-%s.html" % i
        response = urllib.request.urlopen(url)
        with open(file_name, 'wb') as out_file:
            shutil.copyfileobj(response, out_file)

fetch_urls(urls)

我假设您希望将内容保存到单独的文件中,所以我在这里使用enumerate 创建一个唯一的文件名,但您显然可以使用hash()uuid 中的任何内容创建slugs 的模块。

【讨论】:

    【解决方案2】:

    在您的代码中有一些错误:

    • 您使用可变参数列表(错误中的元组)定义 getUrls;
    • 您将 getUrls 参数作为单个变量进行管理(而不是列表)

    你可以试试这段代码

    import urllib2
    import shutil
    
    urls = ['https://www.example.org/', 'https://www.foo.com/', 'http://bar.com']
    def getUrl(urls):
       for url in urls:
          #Only a file_name based on url string
          file_name = url.replace('https://', '').replace('.', '_').replace('/','_')
          response = urllib2.urlopen(url)
          with open(file_name, 'wb') as out_file:
             shutil.copyfileobj(response, out_file)
    getUrl(urls)
    

    【讨论】:

    • 谢谢 - 在我的代码前面,我将文件名设置为 '/path/to/directory' 加上“域”,其中“域”是 URL 中 http://www.com 之间的字符串。该脚本包括 FTP 和推送(通过 Git 到 GitHub 页面),因此我必须设置文件路径,否则 Git 部分将无法工作。无论如何,再次感谢!欣赏!
    【解决方案3】:

    不支持元组:

    urllib.request.urlopen(url[, data][, timeout])
    Open the URL url, which can be either a string or a Request object.
    

    而且你的电话不正确。应该是:

    getUrl(url[0],url[1],url[2])
    

    在函数内部,使用“for u in url”之类的循环遍历所有 url。

    【讨论】:

    • 所以我必须创建多个变量?不幸的。谢谢。我很感激。但是通过列表不是更容易吗?使用 for 循环代替 var[0]、var[0] 等?
    • 不,您应该使用for 循环。
    猜你喜欢
    • 2020-05-15
    • 2016-07-30
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多