【问题标题】:How to scrape all the data from the table which reveals on clicking button?如何从点击按钮时显示的表格中抓取所有数据?
【发布时间】:2021-05-26 13:23:10
【问题描述】:

我正在尝试抓取网站:https://gmatclub.com/forum/decision-tracker.html 我需要获取决策跟踪表 - 实时更新。下面的代码提供了当前页面中的数据

当您向下滚动时,会有一个“显示更多”按钮,可以显示旧条目。从表中获取所有数据的方法是什么。 (所有 5500 多个条目)

import requests
import pandas as pd

with requests.Session() as connection:
    connection.headers.update(
        {
            "referer": "https://gmatclub.com/forum/decision-tracker.html",
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.86 YaBrowser/21.3.0.740 Yowser/2.5 Safari/537.36",
        }
    )
    _ = connection.get("https://gmatclub.com/forum/decision-tracker.html")
    endpoint = connection.get("https://gmatclub.com/api/schools/v1/forum/app-tracker-latest-updates?limit=50&year=all").json()
    for item in endpoint["statistics"]:
        print(item)

#df = pd.DataFrame(endpoint["statistics"])
#print(df.head())
#df.to_csv("your_table_data.csv", index=False)

【问题讨论】:

    标签: python web-scraping python-requests xmlhttprequest


    【解决方案1】:

    解决您的问题的一个快速且简单的方法是在 params.xml 中定义最高限制(您希望获取的数据最多)。我解析了id 只是为了让你知道它有效。您可以坚持使用数据框方法。

    import requests
    
    link = 'https://gmatclub.com/api/schools/v1/forum/app-tracker-latest-updates'
    params = {
        'limit': 500,
        'offset': 0,
        'year': 'all'
    }
    
    with requests.Session() as con:
        con.headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.86 YaBrowser/21.3.0.740 Yowser/2.5 Safari/537.36"
        con.get("https://gmatclub.com/forum/decision-tracker.html")
        while True:
            endpoint = con.get(link,params=params).json()
            if not endpoint["statistics"]:break
            for item in endpoint["statistics"]:
                print(item['id'])
    
            params['offset']+=499
    

    【讨论】:

    • 它没有给我超过 500 个条目。一切都一样。如果我增加限制,什么都不会改变
    • 哇,我认为这非常有效。有后续问题 - 你认为我可以在这里问吗?
    • 当然可以。我会在几个小时后回来。顺便说一句,该站点有时会忽略请求,因此您可能希望有一个 try/except 子句。但是,这是一个不同的问题。
    • 好吧!!是的,我观察到了。非常感谢!
    • 是的,你是对的。根据开发工具,这就是我们得到的。但是,我们可以找到一些其他的东西来使用这些偏移键,这就是我发现的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多