【问题标题】:how do i scrape the web pages where page number is not shown in URL, or no link is provided如何抓取 URL 中未显示页码或未提供链接的网页
【发布时间】:2020-02-05 11:58:54
【问题描述】:

您好,我需要抓取以下网页,但是在分页时,我无法获取带有页码的 URL。 https://www.nasdaq.com/market-activity/commodities/ho%3Anmx/historical

【问题讨论】:

    标签: python-3.x selenium-webdriver web-scraping beautifulsoup


    【解决方案1】:

    您可以通过 api 遍历每个页面(或者在此 api 的情况下为偏移量 - 因为它告诉您总共有多少条记录)。取总记录,然后除以限制集(并使用math.ceiling 进行四舍五入。然后使用倍数作为限制的偏移量作为参数迭代从 1 到该数字的范围)。

    或者,更简单,将限制调整为更高的值,然后在一个请求中获取:

    import requests
    from pandas.io.json import json_normalize
    
    url = 'https://api.nasdaq.com/api/quote/HO%3ANMX/historical'
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
    payload = {
    'assetclass': 'commodities',
    'fromdate': '2020-01-05',
    'limit': '9999',
    'todate': '2020-02-05'}
    
    data = requests.get(url, headers=headers,params=payload).json()
    df = json_normalize(data['data']['tradesTable']['rows'])
    

    输出:

    print (df.to_string())
         close        date    high     low    open   volume
    0   1.5839  02/04/2020  1.6179  1.5697  1.5699   66,881
    1   1.5779  02/03/2020  1.6273  1.5707  1.6188   62,146
    2   1.6284  01/31/2020  1.6786  1.6181  1.6677   68,513
    3    1.642  01/30/2020   1.699  1.6305  1.6952   70,173
    4   1.7043  01/29/2020  1.7355  1.6933  1.7261   69,082
    5   1.7162  01/28/2020  1.7303    1.66   1.674   79,852
    6   1.6829  01/27/2020  1.7305  1.6598  1.7279   97,184
    7   1.7374  01/24/2020  1.7441  1.7369  1.7394   80,351
    8   1.7943  01/23/2020  1.7981  1.7558  1.7919   89,084
    9   1.8048  01/22/2020   1.811  1.7838  1.7929   90,311
    10  1.8292  01/21/2020  1.8859  1.8242  1.8782   53,130
    11  1.8637  01/17/2020   1.875  1.8472  1.8669   79,766
    12  1.8647  01/16/2020  1.8926  1.8615  1.8866   99,020
    13  1.8822  01/15/2020  1.9168  1.8797  1.9043   92,401
    14  1.9103  01/14/2020  1.9224  1.8848   1.898   62,254
    15   1.898  01/13/2020    1.94  1.8941  1.9366   61,328
    16  1.9284  01/10/2020    1.96  1.9262  1.9522   67,329
    17  1.9501  01/09/2020  1.9722  1.9282  1.9665   73,527
    18  1.9582  01/08/2020  1.9776  1.9648  1.9759  110,514
    19  2.0324  01/07/2020  2.0392  2.0065  2.0274   72,421
    20  2.0339  01/06/2020   2.103  2.0193  2.0755   87,832
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多