Python_Requests_报错解决

目录

        • 1、报错:SSLError
        • 2、警告:InsecureRequestWarning
        • 3、报错:socket.timeout
        • 4、报错:UnicodeEncodeError: 'latin-1' codec can't encode characters

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"

你可能感兴趣的:(#,Bug)