python请求头如何自定义?
发布时间:2024-08-21 22:19:57

1、说明
可以使用自定义请求头 headers 该参数将由HTTP头部组成的字典传递给get()。
2、实例
以前的搜索请求可以通过Accept中指定的文本匹配媒体类型来改变,以突出结果中匹配的搜索词:
importrequests
response=requests.get(
'https://api.github.com/search/repositories',
params={'q':'requests+language:python'},
headers={'Accept':'application/vnd.github.v3.text-match+json'},
)
#Viewthenew`text-matches`arraywhichprovidesinformation
#aboutyoursearchtermwithintheresults
json_response=response.json()
repository=json_response['items'][0]
print(f'Textmatches:{repository["text_matches"]}')Accept 告诉服务器您的应用程序可以处理哪些内容类型。 因为要突出匹配的搜索词,所以用的是 application / vnd.github.v3.text-match + json,这是GitHub专有的 Accept 标头,其内容是特殊的JSON格式。
以上是python请求头自定义的方法,希望对每个人都有帮助。更多Python学习指导:python基础教程
本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。
下一篇 python响应头部是什么
