回到顶部

CONTENTS

python3 requests 运行时提示 InsecureRequestWarning

整条错误日志:

​​​​​​​/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 要加上 )

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()

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)

 

^_^
请喝咖啡 ×

前一篇: django 设置中间件(middleware),禁止某些用户访问站点
下一篇: 用百度搜索获取 IP 地址的归属地