urllib2 connection timed out error

北慕城南 提交于 2019-12-10 18:05:54

问题


I am trying to open a page using urllib2 but i keep getting connection timed out errors.
The line which i am using is: f = urllib2.urlopen(url)

exact error is:

URLError: <urlopen error [Errno 110] Connection timed out>


回答1:


urllib2 respects robots.txt. Many sites block the default User-Agent.

Try adding a new User-Agent, by creating Request objects & using them as arguments for urlopen:

import urllib2

request = urllib2.Request('http://www.example.com/')
request.add_header('User-agent', 'Mozilla/5.0 (Linux i686)')

response = urllib2.urlopen(request)

Several detailed walk-throughs are available, such as http://www.doughellmann.com/PyMOTW/urllib2/




回答2:


As a general strategy, open wireshark and watch the traffic generated by urllib2.urlopen(url). You may be able to see where the error is coming from.



来源:https://stackoverflow.com/questions/3197299/urllib2-connection-timed-out-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!