Python_Requests_報錯解決

1、報錯:SSLError

  • (Caused by SSLError(SSLError(“bad handshake: Error([(‘SSL routines’, ‘tls_process_server_certificate’, ‘certificate verify failed’)])”)))
  • 解決:添加verify=False
import requests
resp = requests.get(url, headers=headers,timeout=15, verify=False)

2、警告:InsecureRequestWarning

  • InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)
  • 解決:添加如下內容即可
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

3、報錯:socket.timeout

  • 可以添加捕獲異常,換代理再試
import socket
import requests
import logging
try:
	resp = requests.get(url, headers=headers,timeout=15,proxies=proxy)
except socket.error as err:
	logging.warning(f"{url} socket_timout")

4、報錯:UnicodeEncodeError: ‘latin-1’ codec can’t encode characters

  • UnicodeEncodeError: ‘latin-1’ codec can’t encode characters in position 69-84: ordinal not in range(256)
  • 原因是原錯誤代碼如下:
name = "大小測試科技公司"
headers['Referer'] = f"https://example.cn/search.jsp?num={name}&code=93209"
  • 解決:將headers的Referer裏面的鏈接,將漢字進行轉化爲 %xx 的形式name>quote(name)
from urllib.parse import quote
name = "大小測試科技公司"
headers['Referer'] = f"https://example.cn/search.jsp?num={quote(name)}&code=93209"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章