您可以尝试这些方法,重点是,如果在获取 url 时出现问题,您通常还想处理遇到的错误。
In [4]: import urllib2
In [5]: def test(url):
...: try:
...: response = urllib2.urlopen(url)
...: except urllib2.HTTPError as e:
...: return e.code,None
...: return response.code,response
In [6]: test('http://www.google.com')
Out[6]:
(200,
<addinfourl at 154469068 whose fp = <socket._fileobject object at 0x92caa2c>>)
In [7]: test('http://www.google.com/foobar')
Out[7]: (404, None)
实际上,您还需要处理urllib2.URLError
:
In [10]: def test(url):
...: try:
...: response = urllib2.urlopen(url)
...: except urllib2.HTTPError as err:
...: return err.code, None
...: except urllib2.URLError as err:
...: return err.reason, None
...: return response.code,response
In [11]: test('http://www.google.foo')
Out[11]: (socket.gaierror(-2, 'Name or service not known'), None)