前言
在下列所有操作开展前,务必导入requests模块:import requests
一、response.text()和response.content()的区别
import requests response =requests.get('https://www.baidu.com') print(response.text()) #返回的是unicode格式
import requests response =requests.get('http://www.baidu.com') print(response.content.decode('utf-8')) #返回的二进制格式
想使用text(),则需要进行二进制转化,代码如下修改:
import requests response =requests.get('http://www.baidu.com') response.encoding ='utf-8' #转码成二进制 print(response.text)
二、自定义请求头&自定义get参数
import requests get_param_data ={'wd':'湘江'} #自定义get参数,也可直接写到url里 headerinfos={ 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36', 'Accept':'*/*', 'Accept-Encoding':'gzip, deflate, br', 'Accept-Language':'zh-CN,zh;q=0.9', 'Content-Type':'text/html;charset=utf-8' } #自定义请求头 response =requests.get( url='https://www.baidu.com/s',params=get_param_data,headers=headerinfos) print(response.content.decode('utf-8'))
三、jsonpath取值
import requests import json import jsonpath response =requests.get('https://api.weixin.qq.com/cgi-bin/tags/get?access_token=33_ktUn5mkFTAPn02E-nLB8rbw4OOzhgj_J7olYmP9d9uRWwPjrlNNeAQaLcyGqUHC7KdYH3t8eBQtUs33JdPIQH8n1TsLNHzv-_b2aEt_M-KwnGo2SVLMGDWDeFig6azt_KIfaQMy0d1MHO7v3KFJdAIAAVZ') str =response.content.decode('utf-8') # print(str) value =json.loads(str) #将字符串转换成字典格式 value0 =jsonpath.jsonpath(str,'$.tag[4].name') print(value0)
从下面这段字符串中获取name值
{"tags":[{"id":2,"name":"星标组","count":0},{"id":100,"name":"广东","count":0},{"id":101,"name":"{{上海name_value}}","count":0},{"id":102,"name":"深圳","count":0},{"id":103,"name":"上海","count":0},{"id":104,"name":"佛山","count":0},{"id":105,"name":"郴州","count":0},{"id":106,"name":"李","count":0},{"id":107,"name":"浙江","count":0},{"id":108,"name":"永州","count":0},{"id":109,"name":"耒阳","count":0},{"id":110,"name":"山西","count":0},{"id":111,"name":"西","count":0},{"id":112,"name":"d西","count":0},{"id":114,"name":"ds西","count":0},{"id":115,"name":"cc西","count":0},{"id":116,"name":"ee西","count":0},{"id":117,"name":"张","count":0},{"id":118,"name":"王","count":0},{"id":119,"name":"{{33name_value}}","count":0},{"id":120,"name":"大","count":0},{"id":121,"name":"黄","count":0},{"id":122,"name":"陈","count":0},{"id":123,"name":"{{name_value}}","count":0},{"id":124,"name":"{wq}","count":0},{"id":125,"name":"{???01}","count":0},{"id":126,"name":"???01","count":0},{"id":127,"name":"7uh7uuuuuuuluuuuluu7uuu","count":0},{"id":128,"name":"huh7uul7lhlhuuuu7uluulu","count":0},{"id":129,"name":"hlu7huuluu77uuuu7uu7uuu","count":0},{"id":130,"name":"ulu7uuu7hu77uuuuu7uu7u7","count":0},{"id":131,"name":"7uuu7u7lh7u7uu7u7uu7uu7","count":0},{"id":132,"name":"77uu77777uluu7l7huul7uu","count":0}]}