urllib2如果可能,请考虑使用。它比Windows更加先进和易于使用
urllib。
您可以轻松检测到任何HTTP错误:
>>> import urllib2>>> resp = urllib2.urlopen("http://google.com/abc.jpg")Traceback (most recent call last):<<MANY LINES SKIPPED>>urllib2.HTTPError: HTTP Error 404: Not Foundresp实际上是
HTTPResponse对象,您可以使用以下方法做很多有用的事情:
>>> resp = urllib2.urlopen("http://google.com/")>>> resp.pre200>>> resp.headers["content-type"]'text/html; charset=windows-1251'>>> resp.read()"<<ACTUAL HTML>>"


