![](/img/trans.png)
[英]Syntax Error in Python Requests library for the identifier “headers”
[英]Getting headers with Python requests library
我正在使用Python請求庫來獲取html頁面的標題並使用它來獲取編碼。 但是請求無法獲得標題的一些鏈接。 對於這種情況,我想使用編碼“utf-8”。 我該如何處理這類案件? 如何處理requests.head返回的錯誤。
這是我的代碼:
r = requests.head(link) #how to handle error in case this fails?
charset = r.encoding
if (not charset):
charset = "utf-8"
當請求無法獲取標頭時我收到錯誤:
File "parsexml.py", line 78, in parsefile
r = requests.head(link)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 74, in head
return request('head', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 40, in request
return s.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 229, in request
r.send(prefetch=prefetch)
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 605, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.standardzilla.com', port=80): Max retries exceeded with url: /2008/08/01/diaries-of-a-freelancer-day-thirty-seven/
您應該將代碼放在try-except塊中,捕獲ConnectionErrors。 像這樣:
try:
r = requests.head(link) //how to handle error in case this fails?
charset = r.encoding
if (not charset):
charset = "utf-8"
except requests.exceptions.ConnectionError:
print 'Unable to access ' + link
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.