简体   繁体   English

Web 刮刀按钮 BeautifulSoup Python

[英]Web scraping Button BeautifulSoup Python

i'm trying to webscrape the span from a button that has a determinated class.我正在尝试从具有确定 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.我想找到每次明显变化的“17”。 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.这些工作与 CSS 非常相似。

p selects an element with the type p . p选择类型为p的元素。

p.example selects an element with type p and class example . p.example选择类型为p和 class example的元素。

p span selects any span inside a p . p span选择p内的任何span

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 .例如, p.example span.foo选择任何带有 class foospan在任何带有 class examplep内。

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. <p class="foo bar">Hello, World!</p> foobar都为 class。

I think I am safe to assume the class sqdOP is unique.我想我可以安全地假设 class sqdOP是独一无二的。 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.现在,发出select和 BeautifulSoup 将返回匹配元素的列表。 If this is the only one, you can safely use [0] to get the first item.如果这是唯一的,您可以安全地使用[0]来获取第一项。 So, the final code to select that span :因此,select 的最终代码span为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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