Created
December 16, 2022 11:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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