0
我试图获取包括3XX的http状态代码,但是从我的代码中我无法打印它。要在Python 3(urllib)中打印http状态代码
下面是代码:
import urllib
import urllib.request
import urllib.error
urls = ['http://hotdot.pro/en/404/', 'http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org', 'http://www.voidspace.org.uk']
fh = open("example.txt", "a")
def getUrl(urls):
for url in urls:
try:
with urllib.request.urlopen(url) as response:
requrl = url
the_page = response.code
fh.write("%d, %s\n" % (int(the_page), str(requrl)))
except (urllib.error.HTTPError, urllib.error.URLError) as e:
requrl = url
print (e.code)
fh.write("%d, %s\n" % (int(e.code), str(requrl)))
getUrl(urls)
有人可以帮助我?
是你真正的问题:如何禁用重定向? (这样'urlopen()'不会自动跟随任何30x重定向?) – jfs
是的,我不希望url被重定向。只需打印响应代码和响应时间即可。 – arjun9916
请参阅[有没有简单的方法来请求在Python中的URL,而不是遵循重定向?](http://stackoverflow.com/q/110498/4279) – jfs