[英]proxies and urls in python urllib2
Here is my code but it is giving me some error which i an unable to solve it. 这是我的代码,但它给了我一些我无法解决的错误。 Even though same code is running fine with a single url and a single proxy but it is not running for the file of proxy and urls .
即使使用单个URL和单个代理可以正常运行相同的代码,但对于代理和url的文件也无法运行。
import urllib2
import time
#bangalore, boston,china
with open('urls.txt') as f:
urls = [line.strip() for line in f]
print "list of urls",urls
with open('proxies.txt') as proxies:
for proxy in proxies:
print proxy
proxy = proxy.rstrip()
print proxy
proxy_handler = urllib2.ProxyHandler(proxy)
opener = urllib2.build_opener(proxy_handler)
urllib2.install_opener(opener)
try:
for url in urls:
request=urllib2.Request(url)
start=time.time()
try:
print "from try block"
response=urllib2.urlopen(urls[0])
response.read(1)
ttfb = time.time() - start
print "Latency:", ttfb
print "Status Code:", response.code
print "Headers:", response.headers
print "Redirected url:", response.url
except urllib2.URLError as e:
print "From except"
print "Error Reason:", e.reason
print "Error Message:", e.message
# print "Redirected URL:", e.url
except urllib2.HTTPError as e:
print e.reason
except Exception,e:
print e
replace with this: 替换为:
proxy = json.loads(proxy.rstrip())
(and import json) (并导入json)
urls.txt lines are like: urls.txt行类似于:
http://www.google.com
proxies.txt lines are like: proxies.txt行类似于:
{"http" : "http://ip:port"}
also this will always refer to the first url as per my comment on your post: 而且根据我对您的帖子的评论,这将始终引用第一个网址:
response=urllib2.urlopen(urls[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.