
我想要抓取这个网站的数据, http://ouhe.aiball365.com/league-center/detail?leagueId=31
每次点击页面的时候, 后台会看到一个 post 请求, "http://backend.aiball365.com/web/leagueSummaryWeb"
我把 header 和 data 复制出来, 模拟一个请求的时候。 post data 为: {"channel":"web","os":"browser","leagueId":"31","season":"2019-2020","round":2}
这个 round 是根据比赛轮数变化的, 因为每一页都是一个新的轮数, 所以也可以认为一页修改一个 round 值。
我写的代码是这样的
import requests import json url = "http://backend.aiball365.com/web/leagueSummaryWeb" headers = { 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,ja;q=0.6', 'Content-Length': '69', 'Content-Type': 'application/json;charset=', 'Host': 'backend.aiball365.com', 'Origin': 'http://ouhe.aiball365.com', 'Proxy-Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36', } for i in range(1, 3): data = {"channel": "web", "os": "browser", "leagueId": "31", "season": "2019-2020", "round": i} respOnse= requests.get(url, headers=headers, data=json.dumps(data)) with open('{}.txt'.format(i), 'w+', encoding='utf-8') as the_file: the_file.write(response.text) 我这个代码应该获取到第一页和第二页的, 但是我实际上获取到的是第 32 页的而且两次获取的内容一样请问是怎么回事?
1 ClericPy Jul 1, 2020 要么把 for 循环放在 with open 里面 要么把 'w+', 改成 'a' 不要想当然以为 w+ 就是追加 甚至... 你 print 出来都会发现是变的 |
2 woshichuanqilz OP @ClericPy ``` data = {"channel": "web", "os": "browser", "leagueId": "31", "season": "2019-2020", "round": 1} respOnse= requests.get(url, headers=headers, data=json.dumps(data)) with open('{}.txt'.format(1), 'w+', encoding='utf-8') as the_file: the_file.write(response.text) data = {"channel": "web", "os": "browser", "leagueId": "31", "season": "2019-2020", "round": 2} respOnse= requests.get(url, headers=headers, data=json.dumps(data)) with open('{}.txt'.format(2), 'w+', encoding='utf-8') as the_file: the_file.write(response.text) ``` 我这边这么弄的两次结果都是一样的 |
3 ClericPy Jul 1, 2020 @woshichuanqilz 对不住, 眼花了看错, 你没错 你这个请求是点击赛程赛果的 英超 第 x 轮 的吗, 我抓包不是那个... 甚至没找到你这个请求, 只看到个 leagueMatchRoundWeb 的 |
4 ClericPy Jul 1, 2020 把你的 URL 换成 url = "http://backend.aiball365.com/web/leagueMatchRoundWeb" 试试 如果提交 json 的时候, 其实参数里直接 json = dict_data 就可以了, requests 会帮你转 JSON |
5 woshichuanqilz OP @ClericPy 我直接 f12 看的 我问下你这个链接怎么看到的? 用的什么抓包工具? 你不是看的开发者页面吗 |
6 woshichuanqilz OP @ClericPy 我就是在网页上上直接点击一个页面比如 31 然后开发者页面网络那块就出来我说的这个 post 连接了 |
7 ClericPy Jul 1, 2020 @woshichuanqilz 我是看 chrome 开发者工具里 Network 啊, 你的我反而看不到, 可能不同系统? 我 win10 chrome 最新 |
8 woshichuanqilz OP @ClericPy 能不能发下你的代码谢谢 |
9 ClericPy Jul 2, 2020 https://paste.ubuntu.com/p/j23fZqPnqV/ 就是你的代码改了 url 啊... 没区别都 |
10 summerwar Jul 2, 2020 via iPhone 到底是 post 还是 get ?前面说是 post 请求,代码里直接 requests.get |
11 krixaar Jul 2, 2020 同上,url 只看到一个 leagueMatchRoundWeb,换了 url 一切正常 |