I am trying to iterate over all the column headers of row[0] then remove the unicode symbol and the link number after the planet name. Right now my code looks like this:-
URL_solar_system = "https://en.wikipedia.org/wiki/List_of_gravitationally_rounded_objects_of_the_Solar_System"
from bs4 import BeautifulSoup
import requests
html_content = requests.get(URL_solar_system).text
soup_solar = BeautifulSoup(html_content, "lxml")
tables = soup_solar.find_all('table', attrs={'class': 'wikitable'})
planets = tables[2]
rows = planets.find_all('tr')
headers = [th.text.strip() for th in rows[0].find_all('th') if th.get_text().strip() != '' ]
print('headers: {}'.format(headers))
OUTPUT
headers: ['*Mercury[6][7]', '*Venus[8][9]', '*Earth[10][11]', '*Mars[12][13]', '°Jupiter[14][15]', '°Saturn[16][17]', '‡Uranus[18][19]', '‡Neptune[20][21]']
I am trying to achieve the output as shown below. Desired Output:
headers: ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
Hint for writing the code:-
- headers = []
- for loop that will loop through row[0] with a find all on 'th'
- first_variable = uses your iterator (let's say we call it i in the for loop definition) and uses the text method on it
- second_variable = find this value '[' from the first variable that has the text method redefine the first variable by indexing the second element to the second variable element you defined.
- if statement for that label where is appends the first variable to the headers list you created.