【发布时间】:2021-03-27 13:33:54
【问题描述】:
对于使用beautifulsoup 的项目,我需要从该站点https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue 获取“特斯拉季度收入”表。我认为我准确地获取了初始 html 数据,但我不确定“Tesla Quarterly Revenue”这个短语附加到哪个标签,我认为它可能在 thead 下但不会输出表格。
r=requests.get( 'https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue')
html_data=r.text
soup=BeautifulSoup(html_data)
#print(soup.prettify())
table_=soup.find_all('thead','Tesla Quarterly Revenue')
table_row=table_.find_all('tr')
for row in table_row:
col = row.find_all("td")
date =col[0].text
revenue =col[1].text
tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
tesla_revenue.head()
这里的汤输出
<div class="col-xs-6">
<table class="historical_data_table table">
<thead>
<tr>
<th colspan="2" style="text-align:center">
Tesla Quarterly Revenue
<br/>
<span style="font-size:14px;">
(Millions of US $)
</span>
</th>
</tr>
</thead>
我知道我可以选择整个区域使用
soup.find_all('div',class_='col-xs-6')
但是这个标签下有多个表格,我不确定如何进一步完善它。感谢您的帮助。
【问题讨论】:
-
我认为我可以通过这种方式抓取桌子 table_=soup.find_all('table') table_=table_[1] ,但这需要在多个点进行检查才能获得桌子编号(并且知道它的存在)
标签: python html python-3.x beautifulsoup