import requests
import urllib3
from time import sleep
from sys import argv
script, filename = argv
http = urllib3.PoolManager()
datafile = open('datafile.txt','w')
crawl = ""
with open(filename) as f:
mylist = f.read().splitlines()
def crawlling(x):
for i in mylist:
domain = ("http://" + "%s") % i
crawl = http.request('GET','%s',preload_content=False) % domain
for crawl in crawl.stream(32):
print crawl
sleep(10)
crawl.release_conn()
datafile.write(crawl.status)
datafile.write('>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
datafile.write(crawl.data)
datafile.close()
return x
crawlling(crawl)
_______________________________________________________________________
Extract of domain.txt file:
fjarorojo.info
buscadordeproductos.com
私はpythonの初心者です。私はURLからコンテンツを取得しようとしていますが、エラーが発生しています。さらに、ブラウザで正常に動作しています。 スクリプトの目的は、domain.txtファイルからデータを取得し、それを反復処理して内容を取得し、ファイルに保存することです。Python:HTTPConnectionPool(host = '%s'、port = 80):
Getting this error:
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='%s',
port=80): Max retries exceeded with url:/(Caused by
NewConnectionError('<urllib3.connection.HTTPConnection object at
0x7ff45e4f9cd0>: Failed to establish a new connection: [Errno -2] Name or
service not known',))
'with'ブロックに間違った字下げがあります。' with'ブロック内で 'mylist'、' crawling() '(この関数を呼び出すだけではなく)を定義する必要があります – Andersson