【问题标题】:How do I scrape multiple pages when the url doesn't define the page number?当 url 未定义页码时,如何抓取多个页面?
【发布时间】:2017-06-27 02:32:41
【问题描述】:

我想从网站的第二页获取 html。我习惯了显示页码的 url 并让我通过操纵它来抓取多个页面。

my_url = 'https://www.bodybuilding.com/exercises/finder/lookup/filter/muscle/id/1/muscle/chest'

headers = {'referer':my_url,
           'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
           'x-requested-with':'XMLHttpRequest'}
payload = {'orderByField':'exerciseName',
           'orderByDirection':'ASC',
           'page':30}
params = {'muscleID':1,
          'exerciseTypeID':[2,6,4,7,1,3,5],
          'equipmentID':[9,14,2,10,5,6,4,15,1,8,11,3,7],
          'mechanicTypeID':[1,2,11]}

r = requests.post(my_url, data=payload, headers=headers, params=params, verify=True)
soup = bs(r.text, 'html.parser')

【问题讨论】:

    标签: python web-scraping python-requests


    【解决方案1】:

    这是一个非常有趣的问题。如果您检查页面,您会发现分页是由​​ javascript 驱动的,而不是直接来自任何服务器提供的 API。但是,如果您深入调用堆栈,就会发现它们的内部 API。这意味着(幸运的是),答案比尝试以某种方式呈现页面/在 python 脚本中运行 javascript 简单得多。

    查看exercisesUtil.js,第 1329 行。这应该让您开始使用他们基于 JSON 的 API(当您开始使用它时,您会注意到它仍在 JSON 中传递 HTML 字符串,所以它应该适合就在你漂亮的汤设置中)。有一个名为 refreshFinderResultSet 的函数,其中的 ajax 调用应该可以帮助您入门。

    【讨论】:

    • 哇,谢谢。我只使用了基本网址,但你发给我的链接向我展示了我需要做的一切。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多