【问题标题】:scraping a table from wikipedia with python : can't get a column用 python 从 wikipedia 中抓取表格:无法获取列
【发布时间】:2019-04-20 00:18:11
【问题描述】:

我正在尝试从Wikipedia 刮一张桌子

<tr>
  <td>1</td>
  <td><span class="nowrap"><span class="datasortkey" data-sort-value="Etats unis"><span class="flagicon"><a class="image" href="/wiki/Fichier:Flag_of_the_United_States.svg" title="Drapeau des États-Unis"><img alt="Drapeau des États-Unis" class="noviewer thumbborder" data-file-height="650" data-file-width="1235" height="11" src="//upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Flag_of_the_United_States.svg/20px-Flag_of_the_United_States.svg.png" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Flag_of_the_United_States.svg/30px-Flag_of_the_United_States.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Flag_of_the_United_States.svg/40px-Flag_of_the_United_States.svg.png 2x" width="20" /></a> </span><a href="/wiki/%C3%89tats-Unis" title="États-Unis">États-Unis</a></span></span></td>
  <td>19 390,60 </td>
</tr>

你已经注意到有 3 列,这是我正在使用的代码

A = []
B = []
C = []

for row in DataFondMonetaireInt.findAll("tr"):
    cells = row.findAll("td")
    if len(cells) == 3:
        A.append(cells[0].find(text=True))
        B.append(cells[1].find(text=True))
        C.append(cells[2].find(text=True))

它适用于 A 和 C 但不适用于 B,我无法获得国家名称(在示例中:Etats Unis

为什么它不起作用?

提前谢谢你,

【问题讨论】:

    标签: python python-3.x web-scraping html-table beautifulsoup


    【解决方案1】:

    您也可以使用Wikipedia API 获取WikiText 数据:

    import requests
    import wikitextparser as wtp
    import re
    
    r = requests.get(
        'https://fr.wikipedia.org/w/api.php',
        params = {
            'action': 'parse',
            'page': 'Liste_des_pays_par_PIB_nominal',
            'contentmodel': 'wikitext',
            'prop': 'wikitext',
            'format': 'json'
        }
    )
    
    data = wtp.parse(r.json()['parse']['wikitext']['*'])
    
    f = re.compile(r'[0-9]+[.[0-9]+]?')
    
    for i in range(1, 4):
        print([
            (t[0], wtp.parse(t[1]).templates[0].name, float(f.findall(t[2])[0]))
            for t in data.tables[i].data()
            if len(wtp.parse(t[1]).templates) > 0
        ])
    

    以上将使用WikiTextParser library为您提供3个表中的数据

    【讨论】:

      【解决方案2】:

      您可以执行以下操作来获取每个表

      import pandas as pd
      tables = pd.read_html("https://fr.wikipedia.org/wiki/Liste_des_pays_par_PIB_nominal")
      [tables[i] for i in range(3)]
      

      【讨论】:

        【解决方案3】:

        使用.text 而不是.find(text=True)

        DataFondMonetaireInt = BeautifulSoup(html_text, "html.parser")
        
        A = []
        B = []
        C = []
        
        for row in DataFondMonetaireInt.findAll("tr"):
            cells = row.findAll("td")
            if len(cells) == 3:
                A.append(cells[0].text)
                B.append(cells[1].text.strip())
                C.append(cells[2].text)
        

        【讨论】:

        • 欢迎您,请考虑将此答案标记为正确
        猜你喜欢
        • 2021-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        • 1970-01-01
        • 2015-11-27
        • 1970-01-01
        相关资源
        最近更新 更多