AttributeError:“module”对象在urllib3中没有属性“urlopen”

2024-06-25 22:59:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我也查过其他帖子,但解决办法似乎没有奏效。我一直得到AttributeError:“module”对象没有属性“urlopen”错误。如果你有什么想法,我会非常感激的。在

from lxml import html
import requests
import urllib3

page = requests.get('http://www.sfbos.org/index.aspx?page=18701')
tree = html.fromstring(page.content)

#This will create a list of buyers:
proposal_doc_date = tree.xpath('//ul[@title="Date List"]/li/a/text()')
pdf_url = tree.xpath('//ul[@title="Date List"]/li/a/@href')

print 'Proposal Date ', proposal_doc_date
print 'Proposal PDF ', pdf_url

def download_pdf(url_list):
    for i in url_list:
        response = urllib3.urlopen(i)
        file = open(proposal_doc_date[i], 'wb')
        file.write(response.read())
        file.close()
        print("Completed")

download_pdf(pdf_url)

Tags: importtreeurldatedocpdfhtmlpage
2条回答

您同时导入了requests和{a2},它们的目的相同(请求构建在urllib3之上)。以下是如何使用请求执行此操作:

import requests

# ...

http = requests.Session()

def download_pdf(url_list):
    for i in url_list:
        response = http.get(i)
        file = open(proposal_doc_date[i], 'wb')
        file.write(response.content)
        file.close()
        print("Completed")

和urllib3中的类似场景:

^{2}$

对于流式请求和在文件流式传输时将响应写入文件,还有其他各种各样的事情可以做。查看相应项目的文档了解更多。在

urlopen似乎是urllib3的方法。connectionpool.HTTPConnectionPool班级

https://urllib3.readthedocs.io/en/latest/pools.html#urllib3.connectionpool.HTTPConnectionPool.urlopen

相关问题 更多 >