Issue
So there are 3 text inside a tag, But I need to extract only single tag below is my code of what I have written as
import requests
from bs4 import BeautifulSoup
source= requests.get('eg.com')
soup =BeautifulSoup(source,'lxml')
article= soup.find('div',class_='content')
b = article.li.a.text
which is returning all text inside tag ,OUTPUT:
Apple
2 iteams
but I need only first text i.e Apple HTML CODE AS FOLLOWS
<li class ="iteam">
<a href="eg.com">
" Apple "
<span class ="count">
::before
"2"
<span class ="countlabel">items</span>
::after
</span>
</a>
</li>
Solution
You can use .contents[0]
to get first content inside the <a>
tag (in this case the text " Apple "
):
from bs4 import BeautifulSoup
html_doc = """
<li class ="iteam">
<a href="eg.com">
" Apple "
<span class ="count">
::before
"2"
<span class ="countlabel">
items</span>
::after
</span>
</a>
</li>"""
soup = BeautifulSoup(html_doc, "html.parser")
txt = soup.find(class_="iteam").a.contents[0].strip()
print(txt)
Prints:
" Apple "
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.