2016-05-15 17 views
-1

他の投稿をチェックしましたが、解決策が機能していないようです。私はAttributeErrorを得続けます: 'module'オブジェクトには属性 'urlopen'がありません。これがうまくいかない理由があれば、大変感謝しています。AttributeError: 'module'オブジェクトにurllib3の属性 '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) 
+3

すでにリクエストがありますが、なぜurllib3を使用していますか? –

+0

私はこれで非常に新しいので、おそらくそれは愚かなことです。どのようにしてPDFのダウンロード要求を使用することをお勧めしますか? – tonestrike

+2

とにかく、urllib3にはモジュールにurlopenがありません。適切な使用方法については、マニュアルを参照してください。しかし、Urllib2はそうです。 https://urllib3.readthedocs.io/en/latest/ –

答えて

0

のようです。ここでは、この使用して要求を行う方法は次のとおりです。

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") 

と同様のシナリオ:

import urllib3 

# ... 

http = urllib3.PoolManager() 

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

あなたが要求をストリーミングし、それとしてファイルに応答を書き込むことで行うことができ、他のあらゆる種類のものがあります。ストリーム。詳しくはそれぞれのプロジェクトのドキュメントを参照してください。

関連する問題