Steve Kundukulangara
Steve Kundukulangara

Reputation: 77

Web scraping blank results

I am trying to load data but it returns empty list, not sure what is going wrong here... I am adding the same script which I found from other tutorials

 from bs4 import BeautifulSoup

import requests
import pandas as pd

url = ' https://en.wikipedia.org/wiki/All-time_Olympic_Games_medal_table'

result = requests.get(url)

soup = BeautifulSoup(result.content, 'html.parser')

# print(soup.title)

content = soup.find_all('table', class_='wikitable sortable jquery-tablesorter')

print(content)

Upvotes: 0

Views: 85

Answers (2)

Ram
Ram

Reputation: 4789

Answer based on the Updated URL.

You are selecting all the tables using find_all(). In the URL, you need to select only the medals table and not all.

You can select the medals table with content.find('table', class_='wikitable').

Here I have pulled all the <tr>s from the table and stored them in trs variable. You can iterate over trs list and extract the data you need.

from bs4 import BeautifulSoup

import requests
import pandas as pd

url = 'https://en.wikipedia.org/wiki/All-time_Olympic_Games_medal_table'

result = requests.get(url)

soup = BeautifulSoup(result.text, 'html.parser')

content = soup.find('table', class_='wikitable')
trs = content.findAll('tr')

Upvotes: 2

BubbleMaster
BubbleMaster

Reputation: 184

print(soup) shows that url was scraped successfully. The error is on the content = ... line - the given url contains no table. Here's the print(soup) output:

{"type":"standard","title":"Summer Olympic Games","displaytitle":"Summer Olympic Games","namespace":{"id":0,"text":""},"wikibase_item":"Q159821","titles":{"canonical":"Summer_Olympic_Games","normalized":"Summer Olympic Games","display":"Summer Olympic Games"},"pageid":27566,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Olympic_rings_without_rims.svg/320px-Olympic_rings_without_rims.svg.png","width":320,"height":148},"originalimage":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Olympic_rings_without_rims.svg/342px-Olympic_rings_without_rims.svg.png","width":342,"height":158},"lang":"en","dir":"ltr","revision":"1035750198","tid":"667065b0-eedc-11eb-83d5-e721135a5a1e","timestamp":"2021-07-27T13:12:59Z","description":"International multi-sport event","description_source":"local","content_urls":{"desktop":{"page":"https://en.wikipedia.org/wiki/Summer_Olympic_Games","revisions":"https://en.wikipedia.org/wiki/Summer_Olympic_Games?action=history","edit":"https://en.wikipedia.org/wiki/Summer_Olympic_Games?action=edit","talk":"https://en.wikipedia.org/wiki/Talk:Summer_Olympic_Games"},"mobile":{"page":"https://en.m.wikipedia.org/wiki/Summer_Olympic_Games","revisions":"https://en.m.wikipedia.org/wiki/Special:History/Summer_Olympic_Games","edit":"https://en.m.wikipedia.org/wiki/Summer_Olympic_Games?action=edit","talk":"https://en.m.wikipedia.org/wiki/Talk:Summer_Olympic_Games"}},"extract":"The Summer Olympic Games, also known as the Games of the Olympiad, are a major international multi-sport event normally held once every four years. The Games were first held in 1896 in Athens, Greece, and are currently being held in 2021 in Tokyo, Japan. The International Olympic Committee (IOC) organises the Games and oversees the host city's preparations. In each Olympic event, gold medals are awarded for first place, silver medals are awarded for second place, and bronze medals are awarded for third place; this tradition began in 1904. The Winter Olympic Games were created out of the success of the Summer Olympics.","extract_html":"

The Summer Olympic Games, also known as the Games of the Olympiad, are a major international multi-sport event normally held once every four years. The Games were first held in 1896 in Athens, Greece, and are currently being held in 2021 in Tokyo, Japan. The International Olympic Committee (IOC) organises the Games and oversees the host city's preparations. In each Olympic event, gold medals are awarded for first place, silver medals are awarded for second place, and bronze medals are awarded for third place; this tradition began in 1904. The Winter Olympic Games were created out of the success of the Summer Olympics.

"}

Upvotes: 1

Related Questions