【问题标题】:How to Scrape table from Python Beautifulsoup/API如何从 Python Beautifulsoup/API 中抓取表格
【发布时间】:2021-02-22 05:23:56
【问题描述】:

我正在尝试从https://www.cnbc.com/sector-etfs/ 刮桌子

我使用了类似下面的代码但没有用..

source = urllib.request.urlopen('https://www.cnbc.com/sector-etfs/').read()
soup = bs.BeautifulSoup(source,'lxml')
table = soup.find_all('table')
pd.read_html(str(table))[0]

我还尝试了 requests.get 函数来获取数据,但无法转换为表格格式。您能帮我了解如何以表格格式抓取数据吗?那将不胜感激。

【问题讨论】:

    标签: python api web-scraping beautifulsoup


    【解决方案1】:

    页面源代码中没有表格。数据来自 XHR。尝试以下获取数据:

    import requests
    
    url = 'https://quote.cnbc.com/quote-html-webservice/quote.htm?noform=1&partnerId=2&fund=1&exthrs=0&output=json&symbolType=issue&symbols=289752|289759|289840|289776|136746|289783|289847|289856|289812|289745|5064448|171658|4114925|4733797|281752|21583731|171610|42634418|5064445|5064446|42634422|151687|3585851|5064447|29549437&requestMethod=extended'
    data = requests.get(url).json()
    print(data)
    

    【讨论】:

    • 感谢您的回复。正如我所提到的,我已经尝试过这种方式。有没有办法可以获取数据框格式的数据?
    • @Steve.Kim ,数据以 JSON 格式提供。不像 dataframe 格式。您需要将其转换为您喜欢的格式,就像 JavaScript 在 Web 客户端所做的那样。由于您的问题听起来不像 “如何将 JSON 转换为 HTML 表格”,因此请尝试为此问题创建新问题
    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多