Extracting names from Wikipedia bullet lists only returns the first name for each letter

Viewed 127

I am trying to grab all the names from this following Wikipedia page: https://ro.wikipedia.org/wiki/List%C4%83_de_prenume_rom%C3%A2ne%C8%99ti

This is the code I'm running:

from bs4 import BeautifulSoup
import requests

url = 'https://ro.m.wikipedia.org/wiki/List%C4%83_de_prenume_rom%C3%A2ne%C8%99ti'

r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')
wikiName = [x.find('a').text.upper() for x in soup.findAll('div', class_ = 'div-col columns column-count column-count-5')]
for i in wikiName:
    print(i)

I want to preface this that I'm an absolute beginner. I have tried to input different strings after class_, but nothing returns the entire list of names. The only names that get returned are the first from each letter:

ADA
BEATRICE
CAMELIA
DACIANA
ECATERINA
FABIA
etc.

I would appreciate it if somebody could let me know what I have to do in order to get all the names from the page. Thank you very much in advance!

1 Answers

You can try this. Use find_all to get all names and filter the junk out later.

from bs4 import BeautifulSoup
import requests

url = 'https://ro.m.wikipedia.org/wiki/List%C4%83_de_prenume_rom%C3%A2ne%C8%99ti'

r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')
wikiName = [x.find_all('a') for x in soup.find_all('div', class_ = 'div-col columns column-count column-count-5')]
for names in wikiName:
    print([name.text for name in names if name.text != 'wikt' and name.text != '@'])
Related