Python BeautifulSoup scrape tables

后端 未结 3 1283
失恋的感觉
失恋的感觉 2020-12-13 10:05

I am trying to create a table scrape with BeautifulSoup. I wrote this Python code:

import urllib2
from bs4 import BeautifulSoup

url = \"http://dofollow.nets         


        
3条回答
  •  自闭症患者
    2020-12-13 10:56

    The original link posted by OP is dead... but here's how you might scrape table data with gazpacho:

    Step 1 - import Soup and download the html:

    from gazpacho import Soup
    
    url = "https://en.wikipedia.org/wiki/List_of_multiple_Olympic_gold_medalists"
    soup = Soup.get(url)
    

    Step 2 - Find the table and table rows:

    table = soup.find("table", {"class": "wikitable sortable"}, mode="first")
    trs = table.find("tr")[1:]
    

    Step 3 - Parse each row with a function to extract desired data:

    def parse_tr(tr):
        return {
            "name": tr.find("td")[0].text,
            "country": tr.find("td")[1].text,
            "medals": int(tr.find("td")[-1].text)
        }
    
    data = [parse_tr(tr) for tr in trs]
    sorted(data, key=lambda x: x["medals"], reverse=True)
    

提交回复
热议问题