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
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)