0

I am using python 3.5.2.
My question is:

How to write these 3 sentences with requests instead of urllib2 in the demo below?

import urllib2

proxy_type=record[2].lower()
proxy="%s:%s"%(record[0],record[1])

#How to write these 3 sentences with requests instead of urllib2 below?
req=urllib2.Request(url=url)
req.set_proxy(proxy,proxy_type)
response=urllib2.urlopen(req,timeout=30)
code=response.getcode()

Edit:

@MartijnPieters Thanks,there is another question,as follow:

response = requests.get(url, proxies={proxy_type: proxy})
code=response.getcode()

error:

    code=response.getcode()
AttributeError: 'Response' object has no attribute 'getcode'

getcode() belongs to urllib2,
whick function should I use when urllib2 was replaced by requests?

zwl1619
  • 4,002
  • 14
  • 54
  • 110

0 Answers0