Skip to content

Instantly share code, notes, and snippets.

@henryeleonu
Created December 16, 2022 11:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
How can I write a python code to scrape a table from the website, https://en.wikipedia.org/wiki/List_of_airlines_of_the_United_Kingdom
import requests
from bs4 import BeautifulSoup
# send a request to the webpage
response = requests.get("https://en.wikipedia.org/wiki/List_of_airlines_of_the_United_Kingdom")
# parse the HTML content of the webpage
soup = BeautifulSoup(response.content, "html.parser")
# find the table element in the HTML
table = soup.find("table", {"class": "wikitable sortable"})
# iterate through the rows of the table
for row in table.find_all("tr"):
# find all the cells in the row
cells = row.find_all("td")
# if there are cells, print their contents
if cells:
print(cells[0].text, cells[1].text, cells[2].text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment