【问题标题】:Scrape Humble Bundle games using "Requests" module使用“请求”模块抓取 Humble Bundle 游戏
【发布时间】:2019-11-30 23:40:46
【问题描述】:

我实际上是在尝试获取此网页中包含的游戏信息:https://www.humblebundle.com/store/search?sort=discount&filter=onsale

我尝试的第一件事是复制几天前一位用户为帮助我解决类似问题所做的事情,发出 POST 请求以直接访问我需要的网络数据的来源。 Here's the link of that question in case you still don't know what I'm trying to achieve

为此,我首先执行此代码以获取未加载元素的 Web 的 HTML 文件:

import requests

req = requests.get("https://www.humblebundle.com/store/search?sort=discount&filter=onsale")

a = open("humble.txt", "w")
a.write(req.text)
a.close()

它返回给我this code

您可以在 1084 行中注意到一个名为“storefront-constants-json-data”的脚本,它引起了我的注意,因为它具有与页面相关的一些变量是唯一的。然后我想,“嘿,一定有更多关于这个脚本的信息”。我在网络上单击“检查元素”并转到“网络”选项卡。我在每个 JS 文件中搜索了该脚本名称,发现只有一个引用,this one

此时我迷路了,事实上,我什至不知道我是否以正确的方式(因为我不知道任何 JavaScript)。有人可以告诉我获得那些 Humble Bundle 游戏的路径吗?。


Pd:我昨天写了一个类似的问题,但它非常模糊,所以我决定重写它,提供我拥有的所有信息并解释我尝试过的内容。

Pd2:我不想用 Selenium 或类似的模块来做,它们太慢了。

【问题讨论】:

    标签: python-3.x web-scraping python-requests


    【解决方案1】:

    您在网页上看到的数据是通过来自不同 URL 的 AJAX 请求加载的。如果您打开 Network Inspector,您可以看到请求的 URL - 并且数据以 Json 格式返回:

    import requests
    
    data = requests.get('https://www.humblebundle.com/store/api/search?sort=discount&filter=onsale&request=1').json()
    
    from pprint import pprint
    pprint(data)
    

    打印:

    {'num_pages': 245,
     'num_results': 4894,
     'page_index': 0,
     'request': 1,
     'results': [{'content_types': ['game'],
                  'cta_badge': None,
                  'current_price': [0.0, 'EUR'],
                  'delivery_methods': ['download'],
                  'empty_tpkds': {},
                  'featured_image_recommendation': 'https://hb.imgix.net/2e18a2a9316c0136abf25670bf67ed389c855e4f.jpeg?auto=compress,format&fit=crop&h=154&w=270&s=64e2f8ad8654541c0620d8e018fa2025',
                  'full_price': [0.01, 'EUR'],
                  'human_name': 'Crying Suns Demo',
                  'human_url': 'crying-suns-demo',
                  'icon': 'https://hb.imgix.net/2e18a2a9316c0136abf25670bf67ed389c855e4f.jpeg?auto=format&fit=crop&h=64&w=103&s=dcf803da86b9bcf4cd2c0d038ddf16fb',
                  'icon_dict': {'download': {'available': ['windows', 'mac'],
                                             'unavailable': ['linux']}},
                  'large_capsule': 'https://hb.imgix.net/2e18a2a9316c0136abf25670bf67ed389c855e4f.jpeg?auto=compress,format&fit=crop&h=353&w=616&s=d50b680a5bfd2c6c6acdb4c745db8428',
                  'machine_name': 'cryingsuns_demo_storefront',
                  'non_rewards_charity_split': 0.0,
                  'platforms': ['windows', 'mac'],
                  'rating_for_current_region': 'pegi',
                  'rewards_split': 0.1,
                  'sale_end': 32503708740.0,
                  'sale_type': 'normal',
                  'standard_carousel_image': 'https://hb.imgix.net/2e18a2a9316c0136abf25670bf67ed389c855e4f.jpeg?auto=compress,format&fit=crop&h=206&w=360&s=015688fbe32c7e3e185bdcaddc72e02a',
                  'type': 'product',
                  'xray_traits_thumbnail': 'https://hb.imgix.net/2e18a2a9316c0136abf25670bf67ed389c855e4f.jpeg?auto=compress,format&fit=crop&h=84&w=135&s=eefaf495f9379b213672d82ddeae672a'},
    
    ...and so on.
    

    网络检查员的截图:

    【讨论】:

    • 我明白了,这要简单得多,很高兴知道在无限尝试使用帖子请求之前,非常感谢!
    猜你喜欢
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多