爬蟲中經常出現Traceback (most recent call last):問題解決!!!
問題描述:
當用快速爬取某網站出現經常出現Traceback (most recent call last):的錯誤,也就是連線失敗。原因首先是快速爬取連線時網路不穩定造成的,於是寫了個多次嘗試連線的函式。
錯誤介面:
Traceback (most recent call last):
File "E:/pycharm/PycharmProjects/爬蟲/BG5.py", line 118, in <module>
main(j)
File "E:/pycharm/PycharmProjects/爬蟲/BG5.py", line 84, in main
response1 = getHTMLText(data[j][0])
File "E:/pycharm/PycharmProjects/爬蟲/BG5.py", line 54, in getHTMLText
response = requests.get(url, headers=kv, timeout=60)
File "E:\pycharm\PycharmProjects\venv\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "E:\pycharm\PycharmProjects\venv\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "E:\pycharm\PycharmProjects\venv\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "E:\pycharm\PycharmProjects\venv\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "E:\pycharm\PycharmProjects\venv\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.wzfg.com', port=80): Max retries exceeded with url: /realweb/stat/ProjectListHouseAll.jsp?status=&projectid=9001708&permitNo=%E7%91%9E%E5%AE%89%E5%B8%82%E5%94%AE%E8%AE%B8%E5%AD%97(2017)%E7%AC%AC010%E5%8F%B7 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000000000D42E208>: Failed to establish a new connection: [WinError 10060] 由於連線方在一段時間後沒有正確答覆或連線的主機沒有反應,連線嘗試失敗。',))
解決方法:
def getHTMLText(url):
maxTryNum = 20
for tries in range(maxTryNum):
try:
kv = {"user-agent": "Mizilla/5.0"}
response = requests.get(url, headers=kv, timeout=60)
return response.text
except:
if tries < (maxTryNum - 1):
continue
else:
print("Has tried %d times to access url %s, all failed!" % (maxTryNum, url))
break
相關文章
- Traceback (most recent call last):AST
- Traceback (most recent call last): File "AttributeError: 'NoneType' object has no attribute 'group'ASTErrorNoneObject
- Traceback (most recent call last): File "/usr/bin/firewall-cmd", line 24, in from gi.AST
- 爬蟲常見問題及解決方式爬蟲
- 爬蟲專案常見問題及解決方案爬蟲
- 換IP經常出現的問題及其解決方案
- Python爬蟲程式設計常見問題解決方法Python爬蟲程式設計
- 如何解決Hive中經常出現的資料傾斜問題Hive
- 網路爬蟲編寫常見問題爬蟲
- 爬蟲抓取網路資料時經常遇到的六種問題爬蟲
- 演示PPT文件時常出現的問題與解決
- 使用 JavaScript 解決經典爬樓梯問題JavaScript
- AI|經常崩潰的問題解決AI
- python爬蟲爬取網頁中文亂碼問題的解決Python爬蟲網頁
- python爬蟲常見的那點問題!Python爬蟲
- 網路爬蟲常見問題(個人總結)爬蟲
- 常見網站反爬蟲的解決措施網站爬蟲
- 爬蟲過程中遇到的問題爬蟲
- 解決eclipse編輯jsp、js檔案時,經常出現卡死的問題EclipseJS
- Python小白的爬蟲問題與解決(含程式碼)Python爬蟲
- 爬蟲常見錯誤程式碼及解決措施爬蟲
- Selenium爬蟲遇到超時TimeOut問題的解決方法爬蟲
- 輕鬆解決MacBook經常當機的問題Mac
- 爬蟲抓取UserAgent問題爬蟲
- 好程式設計師Java教程分享Java中經常出現的問題程式設計師Java
- 爬蟲ip代理池搭建前需解決的問題及搭建思路爬蟲
- 爬蟲必備工具,掌握它就解決了一半的問題爬蟲
- 解決SQL Server常見的七個經典問題SQLServer
- 解決app中多語音出現的衝突問題APP
- As常見問題解決方法
- git常見問題解決Git
- 常見問題及解決
- 常見的HTTP介面超時問題出現原因及解決辦法HTTP
- 央行徵信爬蟲解決方案爬蟲
- 解決Ubuntu配置nginx出現的問題UbuntuNginx
- 發現問題,解決問題
- 智慧數字經營的出現能夠解決哪些實際問題?
- myeclispe中向mysql中插入中文資料出現??問題解決辦法LispMySql