get请求,传参数params
response = requests.get(url, headers=headers, params=params)
import requests
# s最后有没有问号结果都一样
url = 'https://www.baidu.com/s?'
# 请求头是一个字典
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Dnt': '1',
'Host': 'www.baidu.com',
'Referer': 'https://www.baidu.com/',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'
}
# 请求参数是一个字典
params = {
'wd': '快递里的经济新脉动',
'sa': 'fyb_hp_news_31065',
'rsv_dl': 'fyb_hp_news_31065',
'from': '31065',
}
# 带上请求参数发起请求,获取响应
response = requests.get(url, headers=headers, params=params)
data = response.content.decode()
print(response.url)
# 执行结果
https://www.baidu.com/s?wd=%E5%BF%AB%E9%80%92%E9%87%8C%E7%9A%84%E7%BB%8F%E6%B5%8E%E6%96%B0%E8%84%89%E5%8A%A8&sa=fyb_hp_news_31065&rsv_dl=fyb_hp_news_31065&from=31065
get直接发送请求,不使用params参数
response = requests.get(url, headers=headers)
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
}
url = 'https://www.baidu.com/s?wd=python'
# url中包含了请求参数,所以此时无需params
response = requests.get(url, headers=headers)
标签:请求,get,url,headers,params,post,requests From: https://www.cnblogs.com/Magiclala/p/17982946