'Can't parse out text that is behind </span>text</a> in Beautifulsoup
I think I have tried it all, read crummy, read documentation on Beautifulsoup4 website. I can't get this thing wrapped around my head.
So to the question:
<a class="ellipsis" href="/aktier/om-aktien.html/5246/investor-a">
<span class="flag small SE"></span>Investor A</a>
<a class="ellipsis" href="/aktier/om-aktien.html/5247/investor-b">
<span class="flag small SE"></span>Investor B</a>
I only want the text behind /span> "text" <a/>
.
This is the code:
def scrape(self):
self.get(const.StockPicks)
html = self.page_source
soup = BeautifulSoup(html, "lxml")
StockPage = soup.find_all("div", class_="orderbookListWrapper")
StockNameBook = []
for StockPages in StockPage:
StockName = StockPages.find_all("a", class_="ellipsis")
StockNameBook.append(StockName)
print (StockNameBook)
What can I try next?
Solution 1:[1]
text behind /span> "text"
are text nodes of a tag
. So You have to select [a class="ellipsis" ] then you can call .get_text()
method to get text nodes value as text/string
html='''
<html>
<body>
<a class="ellipsis" href="/aktier/om-aktien.html/5246/investor-a">
<span class="flag small SE">
</span>
Investor A
</a>
<a class="ellipsis" href="/aktier/om-aktien.html/5247/investor-b">
<span class="flag small SE">
</span>
Investor B
</a>
</body>
</html>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,'lxml')
#print(soup.prettify())
for span in soup.find_all('a',class_="ellipsis"):
txt = span.get_text(strip=True)
print(txt)
Output:
Investor A
Investor B
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | F.Hoque |