Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Include Other Informations in the Scraping Process | Web Scraping
Automating Data Collection from Web Sources

book
Include Other Informations in the Scraping Process

Now that we have understood how the scraping process works let's finish the job and also include the missing information in our process!

Uppgift

Swipe to start coding

  1. Import pandas and initialize an empty df.
  2. Create a for loop to iterate over all tags.
  3. Scrape the country name, the capital city, population, and area.
  4. Append the scraped values in the df.

Lösning

import pandas as pd

col_names = ["Country", "Capital City", "Population", "Area"]
countries = pd.DataFrame(columns = col_names)

for item in soup.find_all("div",{"class":"col-md-4 country"}):
index = len(countries)
countries.loc[index, ["Country"]] = item.find_all("h3", {"class":"country-name"})[0].text.lstrip().rstrip()
countries.loc[index, ["Capital City"]] = item.find_all("span", {"class":"country-capital"})[0].text
countries.loc[index, ["Population"]] = item.find_all("span", {"class":"country-population"})[0].text
countries.loc[index, ["Area"]] = item.find_all("span", {"class":"country-area"})[0].text

countries

Mark tasks as Completed
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 6

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt