Python Requests 库可以使用 headers 参数来设置请求头。例如:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json'
}

response = requests.get('http://example.com', headers=headers)

上面的代码中,headers 参数是一个字典,包含了多个请求头信息,如 User-AgentAccept-LanguageContent-Type 等。使用 requests.get() 方法发送 GET 请求时,将 headers 参数传递进去即可。类似的,使用 requests.post() 方法发送 POST 请求也可以设置请求头。

Python Requests 库:如何设置请求头

原文地址: https://www.cveoy.top/t/topic/mDd3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录