方法1:手动添加cooike
先用浏览器打开首页:'https://xq.com/',此时浏览器会被分配'Cookie'
在headers里加入 'User-Agent'、'Referer'、'Cookie'去请求URL
因为这里是异步加载,因此需要从“Fetch/XHR”的请求中查到“标头”里的请求地址url
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
'Referer': 'https://xq.com/',
'Cookie': 'acw_tc=2760827f17089343372121360e927a734e1bae88ad5caad4dcc5f0863acd62; xq_a_token=76d4e5ee97f60e0be2c9b6c094156d577fba5c5b; xqat=76d4e5ee97f60e0be2c9b6c094156d577fba5c5b; xq_r_token=2537d01490f74c00d7d4a37578ac84f1b7481ca1; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTcxMTA2ODM4MiwiY3RtIjoxNzA4OTM0MzEwMTI0LCJjaWQiOiJkOWQwbjRBWnVwIn0.pjNJMo00_vP3NPARPqF0A5XvgLz6ze-ardiXMrOVgYYGc9GwcPKWtMOMZ2AP7mLAVPtxL2qXe2lxImDZVHiCZj0CO82IXw7aeu0al1y3xyOcMJirBdqt8xiafAYK6LK5kwxj2FYXqh92A4U593dsL0LemMQ698eMOZt_0caQVSBxKPVMRG4NP0rasb-p7ibGwPalToS83_V3mJLAvfOpODdZ5cl9WVuSLY4bJX71iq_J6Xn2cKRD6YE0UZeSTDGVA-upZivpUToDBqMDJ0GvPyx7zdvrg47f3YOO3U6FzgsBt0gQnDDbBn3Hw819rkBnJ0gbZKluQUBjgFebahIvsQ; cookiesu=791708934337219; u=791708934337219; device_id=1c65e977d259e2319d0dae23aaa6f673; acw_sc__v2=65dc4573e4b41a90a2deeb477baa507d676d6142'
}
url = 'https://xq.com/statuses/hot/listV2.json?since_id=-1&max_id=598676&size=15'
response = requests.get(url, headers=headers)
print(response.text)
方法2:用函数接收cookie
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'Referer': 'https://xq.com/',
}
# 第一次访问网址,获取网站返回的cooike
URL = 'https://xq.com/'
response = requests.get(URL, headers=headers)
cookies = dict(response.cookies) # cookie对象转换为字典
URL2 = 'https://xq.com/statuses/hot/listV2.json?since_id=-1&max_id=598676&size=15'
# 使用获得的cookies参数带入请求
response = requests.get(URL2, headers=headers, cookies=cookies)
print(response.text)
如果我们打印cookies可以看到是下面的结果
{'acw_tc': '2760825d17089371882035972e475a290a83627eb47173f2e89dacb95dcb32', 'xq_a_token': '76d4e5ee97f60e0be2c9b6c094156d577fba5c5b', 'xqat': '76d4e5ee97f60e0be2c9b6c094156d577fba5c5b', 'xq_r_token': '2537d01490f74c00d7d4a37578ac84f1b7481ca1', 'xq_id_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTcxMTA2ODM4MiwiY3RtIjoxNzA4OTM3MTQwMjkwLCJjaWQiOiJkOWQwbjRBWnVwIn0.J8HJmRhsusxzYqey6v6CgjxYE-k8gEKFGvoPmbMP6Cid6UZnfzmCIgsx4igqyhnDZImDfXlNqcoRFU50jJmi6e_9CF3gst0Fakl1orY2bH0SUjj3kW_Ut9f4lRruF9EH-4rH1OsYTtv0AgGfp_mvHgnXJyadR_O1wrMGzzDwCrlw_Cgj33997ZlDgxR-cyjR2IKkQQphrGYf3Liyw7L6J8eQxUnLxld255_YcEn2hkE67BBvzXZZjjBTfnEdJKoMlsllqEtaDfhkFNR_DXJnxxi_MczsLjIokk-PxikTJtBnZ_X2dXjSDNOlAIQohuarmuHG2Y_RQjuMHvtdCpL07g', 'cookiesu': '101708937188208', 'u': '101708937188208'}
方法3:使用requests.Session()自动维护cooikes
当你创建了session进程aaa=requests.Session()后,并用这个aaa进程请求了url,那么这个session会自动维护你的cookies,请求中也不用再特殊标识引用,依旧会被代入cookie请求。
requests提供了一个叫做session类,来实现客户端和服务端的会话保持会话保持有两个内涵:
- 保存cookie,下一次请求会带上前一次的cookie
- 实现和服务端的长连接,加快请求速度
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'Referer': 'https://xq.com/',
}
# 第一次访问网址,获取网站返回的cooike
url = 'https://xq.com/'
aaa = requests.Session() # 创建session对象 会自动处理cookie
aaa.get(url, headers=headers) # 用get请求,带着头部拿到返回的cooike等信息
url2 = 'https://xq.com/statuses/hot/listV2.json?since_id=-1&max_id=598676&size=15'
# 使用返回的cookies参数去请求网站
response = aaa.get(url2, headers=headers)
# 由于网站是异步加载,因此可以直接拿取.json文件
print(response.json())
标签:cooike,headers,https,使用,requests,xq,com,id From: https://www.cnblogs.com/Magiclala/p/18034734