简体   繁体   中英

Web scraping Button BeautifulSoup Python

i'm trying to webscrape the span from a button that has a determinated class. This is the code of the page on the website.

<button class="sqdOP yWX7d     _8A5w5    " type="button">altri <span>17</span></button>

I'd like to find "17" that obviously changes everytime. Thanks. I've tried with this one but it doesn't work

for item in soup.find_all('button', {'class': 'sqdOP yWX7d     _8A5w5    '}):

For complex selections, it's best to use selectors . These work very similar to CSS.

p selects an element with the type p .

p.example selects an element with type p and class example .

p span selects any span inside a p .

There are also others, but only these are needed for this example.

These can be nested as you like. For example, p.example span.foo selects any span with class foo inside any p with class example .

Now, an element can have multiple classes, and they are separated by spaces. <p class="foo bar">Hello, World!</p> has both foo and bar as class.

I think I am safe to assume the class sqdOP is unique. You can build the selector pretty easily using the above:

button.sqdOP span

Now, issue select , and BeautifulSoup will return a list of matching elements. If this is the only one, you can safely use [0] to get the first item. So, the final code to select that span :

soup.select('button.sqdOP span')[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM