【发布时间】:2021-03-10 05:06:36
【问题描述】:
我想将 Tesla Quarterly Revenues 中的数据导入 pandas 数据框。我一直在提取年收入表(两个表在网页上并排显示)。我如何需要修改我的代码来提取季度收入?提前致谢。
```html_data = requests.get('https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue').text
soup = BeautifulSoup(html_data, 'html5lib')
tesla_revenue = pd.DataFrame(columns=['Date', 'Revenue'])
for row in soup.find("tbody").find_all("tr"):
col = row.find_all("td")
if (col != []):
date =col[0].text
revenue =col[1].text
tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
tesla_revenue```
【问题讨论】:
-
您可以使用来自 pandas
import pandas as pd;table = pd.read_html('https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue')[1]或soup.select_one('#style-1 div + div .historical_data_table')的 read_html -
在抓取此网站之前,请务必阅读 macrotrends.net/robots.txt。
标签: python pandas web-scraping beautifulsoup