python3 requests 运行时提示 InsecureRequestWarning

2023-02-18 10:38:08 浏览数 (1)

整条错误日志:

代码语言:javascript复制
​​​​​​​/Users/yinzhuoqun/.pyenv/joyoo/lib/python3.6/site-packages/urllib3/connectionpool.py:858: 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)

有警告提示的代码:( verify=False 要加上 )

代码语言:javascript复制
import re
import requests


headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4549.400 QQBrowser/9.7.12900.400"
}
key_word = "hello world"
url = r'https://www.baidu.com/s?wd=%s' % key_word
result = requests.get(url=url, headers=headers, verify=False).text
print(result)

解决办法:

只需要导入 requests 时加上 requests.packages.urllib3.disable_warnings()

代码语言:javascript复制
import re
import requests

requests.packages.urllib3.disable_warnings()


headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4549.400 QQBrowser/9.7.12900.400"
}
key_word = "hello world"
url = r'https://www.baidu.com/s?wd=%s' % key_word
result = requests.get(url=url, headers=headers, verify=False).text
print(result)

0 人点赞