Issue
I'm scraping a website that use data attributes putting the name of the data inside the value along the value itself.
<div data-title="Subscribers: 4,471"></div>
I would like to know how to grab this div
based on partial value that the div should contain "Subscribers", something like this (the * represent the pattern to seek for):
test = soup.find_all("div", {"data-title": "Subscribers"*})
Solution
You could go with css selectors
and check if attribute value:
starts with your pattern
soup.select('[data-title^="Subscribers"]')
ends with your pattern
soup.select('[data-title$="Subscribers"]')
contains your pattern
soup.select('[data-title*="Subscribers"]')
Example
from bs4 import BeautifulSoup
html = '''
<div data-title="Subscribers: 4,471"></div>
<div data-title="Views: 1,000"></div>
'''
soup = BeautifulSoup(html)
soup.select('[data-title^="Subscribers"]')
Answered By - HedgeHog
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.