Python 网页抓取,如何使用 Requests-HTML 库点击 'Next'

Python Web-scraping, How to click 'Next' using Requests-HTML library

我正在尝试使用 python requests-html 模块从“https://fortune.com/global500/2019/search/”获取数据。我能够获得前 100 个项目(从第一页开始),因为该页面启用了 javascript。我们需要点击 "next" 来加载第二页,目前我只得到第一个 100 项。

当我在浏览器上单击 "next" 时,地址栏上的 url 没有变化。所以我不知道如何使用 requests-html.

获取下一页
from requests_html import HTMLSession

def get_fortune500():
    companies = []
    url = 'https://fortune.com/global500/2019/search/'
    session = HTMLSession()
    r = session.get(url)
    r.html.render(wait=1, retries=2)
    table = r.html.find('div.rt-tbody', first=True)
    rows = table.find('div.rt-tr-group')
    for row in rows:
        row_data = []
        cells = row.find('div.rt-td')
        for cell in cells:
            celldata = cell.text.lstrip('$').replace(',', '')
            row_data.append(celldata)
        companies.append(row_data)
    return companies

fortune_list = get_fortune500()
print(fortune_list)
print(len(fortune_list))

非常感谢您的宝贵时间。

这是全部 500 人的名单

https://content.fortune.com/wp-json/irving/v1/data/franchise-search-results?list_id=2666483

本网站将此 API 的响应存储在浏览器 IndexedDB 中,之后只有前端进行控制。

您可以找出读取第一个请求的响应的方法。

虽然您可以通过导航到@Jugraj 提到的 JSON 来做到这一点,但是如果您想了解更多关于请求的 requests-html you can always look for the official documentation-html。