Issue
I am trying to pull the data value slug instead of the rendered website text.
<option value="93" data-value-slug="lager-munich-dunkel">Lager -
Munich Dunkel</option>
#This currently pulls 'Lager Munich Dunkel' instead of 'lager-munich-dunkel'
beer_type = []
for b in beer_type:
beer_style = b.select('option')
beer_row = [i.text for i in beer_style]
beer_type.append(beer_row)
beer_type
I need to pull the data value slug portion of the html so I can use it in the url as lager-munich-dunkel
Solution
Element attributes are converted to dictionary key/value pairs when using the BeautifulSoup module. So, you can get the value you want like so:
# using i.get instead of i[ ] in case there is a default option
# Which may not have the attribute
beer_row = [i.get('data-value-slug','') for i in beer_style]
Answered By - CaffeinatedMike
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.