【发布时间】:2020-05-09 12:06:21
【问题描述】:
我正在尝试构建一个网络抓取工具,用于为我的数据可视化项目创建 covid-19 数据集。我需要https://www.worldometers.info/coronavirus/的这张桌子
import requests
from bs4 import BeautifulSoup
url = "https://www.worldometers.info/coronavirus/"
page = requests.get(url,verify=True)
soup = BeautifulSoup(page.content,features="lxml")
rows = soup.select("tr")
for data in rows:
print(data.text)
我得到了想要的输出,但在每一行(国家)它还显示我不想包含在我的数据集中的大陆名称。有什么解决办法吗? 由于我是网络抓取的新手,我需要我能得到的所有帮助。
更新:这是 html 代码,数据集中不需要最后一个指定“欧洲”的 td。
<tr style="" role="row" class="odd">
<td style="font-weight: bold; font-size:15px; text-align:left;"><a class="mt_a" href="country/uk/">UK</a></td>
<td style="font-weight: bold; text-align:right" class="sorting_1">211,364</td>
<td style="font-weight: bold; text-align:right;"></td>
<td style="font-weight: bold; text-align:right;">31,241 </td>
<td style="font-weight: bold; text-align:right;"></td>
<td style="font-weight: bold; text-align:right">N/A</td>
<td style="text-align:right;font-weight:bold;">179,779</td>
<td style="font-weight: bold; text-align:right">1,559</td>
<td style="font-weight: bold; text-align:right">3,114</td>
<td style="font-weight: bold; text-align:right">460</td>
<td style="font-weight: bold; text-align:right">1,631,561</td>
<td style="font-weight: bold; text-align:right">24,034</td>
<td style="display:none" data-continent="Europe">Europe</td>
</tr>
【问题讨论】:
-
您需要更准确地选择要选择的内容。当前代码选择所有
tr标记而没有更多信息。也许,您只想选择tr元素以获得精确的table? -
除了前面的注释之外,您始终可以在处理时从数据中排除最后一个“td”。
标签: python python-3.x web-scraping beautifulsoup