今天是拿书架上数目数据。主要的点是使用requests.session会话去拿到数据,模拟我们上门户网站时都需要登录这一步的操作。
任务 :
#登录-拿到cookie #带着cookie去请求书架的url-获得书架内容 #必须把以上两个步骤连在一起 #我们可以使用session请求,因为session请求不会把cookie丢失
import requests
#会话
session=requests.session()
常规办法是先登录门户网站,然后到网站书架上获取书目。
#method1
#1.登录
url='https://passport.17k.com/ck/user/login'
data={
"loginName":"17512573261",
"password":"zzy8794400"
}
resp=session.post(url,data=data)
#print(resp.text)
#print(resp.cookies)看cookies
#2.拿书架内容
#刚才的那个session中是有cookies 的
resp1=session.get('https://user.17k.com/ck/author/shelf?page=1&appKey=2406394919')
#print(resp1.json())
方法二就是从上一步登录完之后直接找到cookies,把cookies写进headers里面,无需登录,使用requests.get就可以了
#method2
resp2=requests.get('https://user.17k.com/ck/author/shelf?page=1&appKey=2406394919',headers={
"Cookie":"GUID=73fd4479-458d-482b-a056-0c2ba13ebb15; c_channel=0; c_csc=web; accessToken=avatarUrl%3Dhttps%253A%252F%252Fcdn.static.17k.com%252Fuser%252Favatar%252F02%252F22%252F77%252F93687722.jpg-88x88%253Fv%253D1646975205000%26id%3D93687722%26nickname%3Dzzy879%26e%3D1662798540%26s%3Da9aab8df808d7f53; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2293687722%22%2C%22%24device_id%22%3A%2217f775df8206ec-0ce43dc28137fb-977173c-1024000-17f775df821cc1%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%2273fd4479-458d-482b-a056-0c2ba13ebb15%22%7D"
})
print(resp2.text)