python请求头如何自定义?
python请求头如何自定义?
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'},
)
#Viewthenewtext-matchesarraywhichprovidesinformation
#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学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
python字符串需要注意的语法问题
python字符串需要注意的语法问题语法错误是经常发生的一个错误。当一个程序包含非法的Python代码时,会产生语法错误。举例来说,在包含单引号的...详情>>
2023-11-14 18:23:24
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15热门推荐
python中title函数的作用
沸python中集合与列表有什么区别?
热python中uuid模块是什么?
热python中使用_setattr_()
新python中scikit-learn是什么?
python Faker库如何使用?
python behold库是什么?
python中ndarray是什么?
python UDP编程是什么意思?
python如何创建UDP服务器和客户端?
python字符串需要注意的语法问题
python如何配置Logging处理器
pythonos模块有几种用法?
python如何定制日志输出格式
技术干货






