Search For Text Inside A Tag Using Beautifulsoup And Returning The Text In The Tag After It
I'm trying to parse the follow HTML code in python using beautiful soup. I would like to be able to search for text inside a tag, for example 'Color' and return the text next tag '
Solution 1:
You can define a function to return the value for the key you enter:
def get_txt(soup, key):
key_tag = soup.find('span', text=key).parent
return key_tag.find_all('span')[1].text
color = get_txt(soup, 'Color')
print('Color: ' + color)
features = get_txt(soup, 'Features')
print('Features: ' + features)
Output:
Color: Slate, mykonos
Features: Camera lens cutout, hard shell, rubberized, port cut-outs, raised edges
I hope this is what you are looking for.
Explanation:
soup.find('span', text=key)
returns the <span>
tag whose text=key
.
.parent
returns the parent tag of the current <span>
tag.
Example:
When key='Color'
, soup.find('span', text=key).parent
will return
<divclass="_JDu"><spanclass="_IDu">Color</span><spanclass="_KDu">Slate, mykonos</span></div>
Now we've stored this in key_tag
. Only thing left is getting the text of second <span>
, which is what the line key_tag.find_all('span')[1].text
does.
Solution 2:
Give it a go. It can also give you the corresponding values. Make sure to wrap the html elements
within content=""" """
variable between Triple Quotes to see how it works.
from bs4 import BeautifulSoup
soup = BeautifulSoup(content,"lxml")
for elem in soup.select("._JDu"):
item = elem.select_one("span")
if"Features"in item.text: #try to see if it misses the corresponding values
val = item.find_next("span").text
print(val)
Post a Comment for "Search For Text Inside A Tag Using Beautifulsoup And Returning The Text In The Tag After It"