【问题标题】:attribute error when parsing html table with beautifulSoup4使用 beautifulSoup4 解析 html 表时出现属性错误
【发布时间】:2019-07-13 08:52:48
【问题描述】:

我正在尝试使用BeautifulSoup4 从表格中获取排名、国家名称、人口、密度和表面。当我使用table.find_all('tr') 时,它返回以下错误:

AttributeError: 'ResultSet' 对象没有属性 'find_all'

我猜问题的根源是:

table = soup.find_all("table",attrs={"class":"wikitable sortable"}) 

返回一个元素的列表,其中包含tbody,我无法循环遍历它!

import requests 
from bs4 import BeautifulSoup
import re

r = requests.get("https://fr.wikipedia.org/wiki/Liste_des_pays_par_densit%C3%A9_de_population")

soup = BeautifulSoup(r.content, "html.parser")
table = soup.find_all("table",attrs={"class":"wikitable sortable"})
for i in table.find_all('tr'):

    tds=i.find_all('td', string=True)
    tds_string = str(tds)
    specifications = str(re.sub("<.+?>"," ",tds_string))    
    print(specifications)

表格格式如下:

<table class="wikitable sortable" style="text-align:right">
<tbody><tr>
<th scope="col">Rang
</th>
<th scope="col">Pays <i>(ou territoire)</i>
</th>
<th scope="col">Densité (<abbr class="abbr" title="habitants">hab.</abbr>/km<sup>2</sup>)
</th>
<th scope="col">Population (<abbr class="abbr" title="habitants">hab.</abbr>)
</th>
<th scope="col">Superficie (km<sup>2</sup>)
</th></tr>
<tr>
<td>1</td>
<td align="left"><span class="datasortkey" data-sort-value="Monaco"><span class="flagicon"><a class="image" href="/wiki/Fichier:Flag_of_Monaco.svg" title="Drapeau de Monaco"><img alt="Drapeau de Monaco" class="noviewer thumbborder" data-file-height="600" data-file-width="750" decoding="async" height="16" src="//upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Flag_of_Monaco.svg/20px-Flag_of_Monaco.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Flag_of_Monaco.svg/30px-Flag_of_Monaco.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Flag_of_Monaco.svg/40px-Flag_of_Monaco.svg.png 2x" width="20"/></a> </span><a href="/wiki/Monaco" title="Monaco">Monaco</a></span></td>
<td>18 866</td>
<td>38 109</td>
<td>2,02
</td></tr>
<tr>
<td>2</td>
<td align="left"><span class="datasortkey" data-sort-value="Singapour"><span class="flagicon"><a class="image" href="/wiki/Fichier:Flag_of_Singapore.svg" title="Drapeau de Singapour"><img alt="Drapeau de Singapour" class="noviewer thumbborder" data-file-height="2880" data-file-width="4320" decoding="async" height="13" src="//upload.wikimedia.org/wikipedia/commons/thumb/4/48/Flag_of_Singapore.svg/20px-Flag_of_Singapore.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/4/48/Flag_of_Singapore.svg/30px-Flag_of_Singapore.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/4/48/Flag_of_Singapore.svg/40px-Flag_of_Singapore.svg.png 2x" width="20"/></a> </span><a href="/wiki/Singapour" title="Singapour">Singapour</a></span></td>
<td>7 829</td>
<td>5 423 148</td>
<td>692,7
</td></tr>
<tr>
.....

【问题讨论】:

标签: python web-scraping beautifulsoup html-parsing


【解决方案1】:

find_all 返回一个列表。您可能想使用find,它将返回一个表格。要么在每个表上迭代调用find_all('tr') 的列表。

【讨论】:

    【解决方案2】:

    只有一张桌子,所以请使用 pandas

    import pandas as pd
    table = pd.read_html('https://fr.wikipedia.org/wiki/Liste_des_pays_par_densit%C3%A9_de_population')
    # print(table)
    table[0].to_csv(r'C:\Users\User\Desktop\Data.csv', sep=',', encoding='utf-8-sig',index = False )
    

    【讨论】:

    • 感谢您的回答。我可以拿到桌子,但它不适合终端,我该如何解决?
    • 将其写入 csv?查看我的编辑并告诉我。当然改变文件路径。
    • 这回答了你的问题吗?
    猜你喜欢
    • 2020-04-05
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多