我正在尝试刮网页,在那里我需要解码成一个数据帧的整个表。我用漂亮的汤来做这个。在某些td
标记中,有span
标记没有任何文本。但是这些值显示在网页上的特定span标记中
以下html
代码对应于该网页
<td>
<span class="nttu">::after</span>
<span class="ntbb">::after</span>
<span class="ntyc">::after</span>
<span class="nttu">::after</span>
</td>
但是,这个td
标记中显示的值是23.8
。我试着把它擦掉,但我收到的是空文本
如何使用漂亮的汤刮取这个值
网址:https://en.tutiempo.net/climate/ws-432950.html
我的代码是用于刮取下表所示
http_url = "https://en.tutiempo.net/climate/01-2013/ws-432950.html"
retreived_data = requests.get(http_url).text
soup = BeautifulSoup(retreived_data, "lxml")
climate_table = soup.find("table", attrs={"class": "medias mensuales numspan"})
climate_data = climate_table.find_all("tr")
for data in climate_data[1:-2]:
table_data = data.find_all("td")
row_data = []
for row in table_data:
row_data.append(row.get_text())
climate_df.loc[len(climate_df)] = row_data
误解了您的问题,因为您引用了两个不同的URL。我现在明白你的意思了
是的,奇怪的是,在第二个表中,他们使用CSS填充了一些
<td>
标记的内容。您需要做的是从<style>
标记中取出这些特殊情况。一旦有了这些元素,就可以在html源代码中替换这些元素,并最终将其解析为数据帧。我使用pandas,因为它在引擎盖下使用beautifulsou解析<table>
标记。但我相信这会让你得到你想要的:输出:
相关问题 更多 >
编程相关推荐