[Python] Web Scraping Wikipedia

Discussão em 'Python' iniciado por Stack, Abril 20, 2025.

  1. Stack

    Stack Membro Participativo

    I need bring only the continent (North America) using Wikipedia by URL (in the code below, I will replace the country, in this case, "Guatemala", and make it be a parameter in power BI), but I am getting the whole <a tag. How can I do that?

    import requests as rq
    import pandas as pd
    from bs4 import BeautifulSoup
    import numpy as np
    import re

    url = 'https://en.wikipedia.org/wiki/Geography_of_Guatemala'
    page = rq.get(url)
    soup = BeautifulSoup(page.text, 'html.parser')
    res = soup.find_all('td', class_='infobox-data')
    df = pd.DataFrame(res)
    df = df.to_numpy()
    df = str(df[0])
    print(df)
    print(re.search('\">(.*?)\</a>', df).group(1))


    This is the data frame:

    [<td class="infobox-data"><a href="/wiki/North_America" title="North America">North America</a></td>]


    and this is the re.search:

    <a href="/wiki/North_America" title="North America">North America

    Continue reading...

Compartilhe esta Página