不同的 post value 应该得到不同的页面结果, 得到的结果却是一样的, 请教爬虫问题

2020-07-01 22:54:43 +08:00
 woshichuanqilz

我想要抓取这个网站的数据, 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=utf-8',
    '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 页的而且两次获取的内容一样请问是怎么回事?

1503 次点击
所在节点    Python
11 条回复
ClericPy
2020-07-01 23:00:46 +08:00
要么把 for 循环放在 with open 里面
要么把 'w+', 改成 'a'

不要想当然以为 w+ 就是追加

甚至... 你 print 出来都会发现是变的
woshichuanqilz
2020-07-01 23:16:56 +08:00
@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)
```
我这边这么弄的两次结果都是一样的
ClericPy
2020-07-01 23:32:11 +08:00
@woshichuanqilz 对不住, 眼花了看错, 你没错

你这个请求是点击赛程赛果的 英超 第 x 轮 的吗, 我抓包不是那个... 甚至没找到你这个请求, 只看到个 leagueMatchRoundWeb 的
ClericPy
2020-07-01 23:39:21 +08:00
把你的 URL 换成
url = "http://backend.aiball365.com/web/leagueMatchRoundWeb"
试试

如果提交 json 的时候, 其实参数里直接 json = dict_data 就可以了, requests 会帮你转 JSON
woshichuanqilz
2020-07-01 23:48:35 +08:00
@ClericPy 我直接 f12 看的 我问下你这个链接怎么看到的? 用的什么抓包工具? 你不是看的开发者页面吗
woshichuanqilz
2020-07-01 23:49:50 +08:00
@ClericPy 我就是在网页上上直接点击一个页面比如 31 然后开发者页面网络那块就出来我说的这个 post 连接了
ClericPy
2020-07-01 23:54:59 +08:00
@woshichuanqilz 我是看 chrome 开发者工具里 Network 啊, 你的我反而看不到, 可能不同系统? 我 win10 chrome 最新
woshichuanqilz
2020-07-01 23:57:07 +08:00
@ClericPy 能不能发下你的代码谢谢
ClericPy
2020-07-02 00:04:00 +08:00
https://paste.ubuntu.com/p/j23fZqPnqV/
就是你的代码改了 url 啊... 没区别都
summerwar
2020-07-02 05:25:31 +08:00
到底是 post 还是 get ?前面说是 post 请求,代码里直接 requests.get
krixaar
2020-07-02 08:50:19 +08:00
同上,url 只看到一个 leagueMatchRoundWeb,换了 url 一切正常

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/686392

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX