Using Find_all In Bs4 To Get Text As A List
I'll start by saying I'm very new with Python. I've been building a Discord bot with discord.py and Beautiful Soup 4. Here's where I'm at: @commands.command(hidden=True) async def
Solution 1:
Replace
text = soupObject.find_all("font", attrs={'size': '4'})
with this:
all_font_tags = soupObject.find_all("font", attrs={'size': '4'})
list_of_inner_text = [x.text for x in all_font_tags]
# If you want to print the text as a comma separated stringtext = ', '.join(list_of_inner_text)
Solution 2:
You are returning a list of Tags
from BeautifulSoup, the brackets you are seing are from the list object.
Either return them as a list of strings:
text = [Member.get_text().encode("utf-8").strip() for Member in soup.find_all("font", attrs={'size': '4'}) if not Member.get_text().encode("utf-8").startswith("\xe2")]
Or a single string:
text = ",".join([Member.get_text().encode("utf-8") for Member in soup.find_all("font", attrs={'size': '4'}) if not Member.get_text().encode("utf-8").startswith("\xe2")])
Post a Comment for "Using Find_all In Bs4 To Get Text As A List"