Issue
I want to get some names in a webpage to use them later in the code.
content = requests.get("http://serpadres.com/bebe/los-200-nombres-latinos-mas-populares-de-los-ultimos-tiempos/52175/").content
soup = BeautifulSoup(content, features="html.parser")
for tag in soup.find_all("br"):
print("{0}: {1}".format(tag.name, tag.text))
I tried this and it did print all the names but also the br tags, being the result:
br:
br:
br:
br:
br:
br:
br:
br:
VERY LONG LIST OF NAMES
br:
br:
br:
br:
br:
br:
br:
br:
br:
br:
and many and many more br:. How can I exclude those and also convert the name to strings?
Solution
You can check if it's a name or just blank space by adding this line:
if tag.get_text() != '':
The code would then look like this:
from bs4 import BeautifulSoup
from pip._vendor import requests
mylist = []
content = requests.get("http://serpadres.com/bebe/los-200-nombres-latinos-mas-populares-de-los-ultimos-tiempos/52175/").content
soup = BeautifulSoup(content, features="html.parser")
for tag in soup.find_all('br'):
if tag.get_text() != '':
print(tag.get_text())
Answered By - Yven Ven
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.