Skip to content

Instantly share code, notes, and snippets.

@henryeleonu
Created December 16, 2022 11:29
Show Gist options
  • Save henryeleonu/7d3f86733328e27cc6a56123d97459b4 to your computer and use it in GitHub Desktop.
Save henryeleonu/7d3f86733328e27cc6a56123d97459b4 to your computer and use it in GitHub Desktop.
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