V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
woshichuanqilz
V2EX  ›  Python

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

  •  
  •   woshichuanqilz · 2020-07-01 22:54:43 +08:00 · 1489 次点击
    这是一个创建于 1391 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    11 条回复    2020-07-02 08:50:19 +08:00
    ClericPy
        1
    ClericPy  
       2020-07-01 23:00:46 +08:00
    要么把 for 循环放在 with open 里面
    要么把 'w+', 改成 'a'

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

    甚至... 你 print 出来都会发现是变的
    woshichuanqilz
        2
    woshichuanqilz  
    OP
       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
        3
    ClericPy  
       2020-07-01 23:32:11 +08:00
    @woshichuanqilz 对不住, 眼花了看错, 你没错

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

    如果提交 json 的时候, 其实参数里直接 json = dict_data 就可以了, requests 会帮你转 JSON
    woshichuanqilz
        5
    woshichuanqilz  
    OP
       2020-07-01 23:48:35 +08:00 via Android
    @ClericPy 我直接 f12 看的 我问下你这个链接怎么看到的? 用的什么抓包工具? 你不是看的开发者页面吗
    woshichuanqilz
        6
    woshichuanqilz  
    OP
       2020-07-01 23:49:50 +08:00 via Android
    @ClericPy 我就是在网页上上直接点击一个页面比如 31 然后开发者页面网络那块就出来我说的这个 post 连接了
    ClericPy
        7
    ClericPy  
       2020-07-01 23:54:59 +08:00
    @woshichuanqilz 我是看 chrome 开发者工具里 Network 啊, 你的我反而看不到, 可能不同系统? 我 win10 chrome 最新
    woshichuanqilz
        8
    woshichuanqilz  
    OP
       2020-07-01 23:57:07 +08:00 via Android
    @ClericPy 能不能发下你的代码谢谢
    ClericPy
        9
    ClericPy  
       2020-07-02 00:04:00 +08:00   ❤️ 1
    https://paste.ubuntu.com/p/j23fZqPnqV/
    就是你的代码改了 url 啊... 没区别都
    summerwar
        10
    summerwar  
       2020-07-02 05:25:31 +08:00 via iPhone   ❤️ 1
    到底是 post 还是 get ?前面说是 post 请求,代码里直接 requests.get
    krixaar
        11
    krixaar  
       2020-07-02 08:50:19 +08:00
    同上,url 只看到一个 leagueMatchRoundWeb,换了 url 一切正常
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4831 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 10:01 · PVG 18:01 · LAX 03:01 · JFK 06:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.