码迷,mamicode.com
首页 > 其他好文 > 详细

Requests 小方法

时间:2020-06-26 14:27:40      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:https   windows   arc   ppa   head   insecure   chardet   ica   filter   

返回页面乱码问题

方案一

import requests
headers = {
        ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36‘
}
url = ‘https://search.jd.com/Search?keyword=%E7%AC%94%E8%AE%B0%E6%9C%AC&enc=utf-8&wq=%E7%AC%94%E8%AE%B0%E6%9C%AC‘
res = requests.get(url,headers=headers)

res.encoding = res.apparent_encoding

print(res.text)

方案二

import chardet
import requests
headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36‘
}
url = ‘https://search.jd.com/Search?keyword=%E7%AC%94%E8%AE%B0%E6%9C%AC&enc=utf-8&wq=%E7%AC%94%E8%AE%B0%E6%9C%AC‘
res = requests.get(url,headers=headers)
res.encoding =chardet.detect(res.content)[‘encoding‘]
print(res.text)
print(res.encoding)

忽略警告

使用 verify=False 会提示不安全警告

InsecureRequestWarning: Unverified HTTPS request is being made to host ‘api.growingio.com‘. Adding certificate verification is strongly advised. See:

import warnings
warnings.filterwarnings("ignore")

Requests 小方法

标签:https   windows   arc   ppa   head   insecure   chardet   ica   filter   

原文地址:https://www.cnblogs.com/zl158218/p/13194521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!