在 Python 3 (urllib) 中打印 http 状态代码



我正在尝试获取包括3XX在内的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, %sn" % (int(the_page), str(requrl)))
        except (urllib.error.HTTPError, urllib.error.URLError)  as e:
            requrl = url
            print (e.code)
            fh.write("%d, %sn" % (int(e.code), str(requrl)))
getUrl(urls)

有人可以帮助我吗?

并非所有类URLError的错误都会有code,有些只会有reason

此外,在同一except块中捕获URLErrorHTTPError不是一个好主意(请参阅文档):

def getUrl(urls):
   for url in urls:
        try:
           with urllib.request.urlopen(url) as response:
                log(fh, response.code, url)
        except urllib.error.HTTPError  as e:
            log(fh, e.code, url)
        except urllib.error.URLError as e:
            if hasattr(e, 'reason'):
                log(fh, e.reason, url)
            elif hasattr(e, 'code'):
                log(fh, e.code, url)
 def log(fh, item, url):
     print(item)
     fh.write("%s, %sn" % (item, url))

在代码中实例化以下内容。

   try:
        response = urllib.request.urlopen(url)
        code = response.getcode()
        print(code)
    except Exception as e:
        print(f'Error:  {url} : {str(e)}')

试试 python 的请求包。(文档在这里)

更直接一点,非常容易打印http状态代码。

import requests
URL = 'http://yourURL:8080'
query = {'your': 'dictionary to send'}
response = requests.post(URL, data=query)
return response.status_code

最新更新