Function to include all text in <p> tags but exclude the italicized text from that string
·
Answer a question
I'm trying to scrape a website which has quotes and name of those who quoted it, and after narrowing down to the li tag which has that quote, I tried using the get_text() function but it also includes the italicized text which I don't want
Here is the code which I tried:
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
result = requests.get('https://blog.hubspot.com/sales/famous-quotes')
soup = bs(result.content, 'lxml')
trial = soup.select("div.hsg-featured-snippet li")
print(trial[0].get_text())
The output:
"The greatest glory in living lies not in never falling, but in rising every time we fall." -Nelson Mandela
I want to exclude the 'Nelson Mandela' part. Is there any way?
Answers
You can remove the authors by destroying all <em>
tags using the decmopose()
method:
Tag.decompose()
removes a tag from the tree, then completely destroys it and its contents.
In your example
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
result = requests.get("https://blog.hubspot.com/sales/famous-quotes")
soup = bs(result.content, "lxml")
# Select all authors, and remove them from the `soup`
for tag in soup.select("div.hsg-featured-snippet li em"):
tag.decompose()
trial = soup.select("div.hsg-featured-snippet li")
print(trial[0].get_text())
# Or uncomment the following to remove the additional "-"
# print(trial[0].get_text().split("-")[0])
更多推荐
所有评论(0)