Scrape tables into dataframe with BeautifulSoup

后端 未结 4 873
日久生厌
日久生厌 2020-12-13 21:03

I\'m trying to scrape the data from the coins catalog.

There is one of the pages. I need to scrape this data into Dataframe

So far I have this code:

<
4条回答
  •  情书的邮戳
    2020-12-13 21:20

    Try:

    import pandas as pd
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html, "html.parser")
    table = soup.find('table', attrs={'class':'subs noBorders evenRows'})
    table_rows = table.find_all('tr')
    
    res = []
    for tr in table_rows:
        td = tr.find_all('td')
        row = [tr.text.strip() for tr in td if tr.text.strip()]
        if row:
            res.append(row)
    
    
    df = pd.DataFrame(res, columns=["Year", "Mintage", "Quality", "Price"])
    print(df)
    

    Output:

       Year  Mintage Quality    Price
    0  1882  108,000     UNC        —
    1  1883  786,000     UNC  ~ $4.03
    

提交回复
热议问题