Requests 小方法

Posted zl158218

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Requests 小方法相关的知识,希望对你有一定的参考价值。

返回页面乱码问题

方案一

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 小方法的主要内容,如果未能解决你的问题,请参考以下文章

VS中添加自定义代码片段——偷懒小技巧

Requests 小方法

微信小程序代码片段

Bug解决requests.exceptions.ReadTimeout &http.client.RemoteDisconnected: Remote end closed connecti

简单的方法来分享/讨论/协作的代码片段?

更多Requests的小技巧以及总结