美丽的汤find_all找不到具有多个类的CSS选择器 [英] Beautiful soup find_all doesn't find CSS selector with multiple classes

查看:161
本文介绍了美丽的汤find_all找不到具有多个类的CSS选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站上有此<a>元素

<a role="listitem" aria-level="1" href="https://www.rest.co.il" target="_blank" class="icon rest" title="this is main title" iconwidth="35px" aria-label="website connection" style="width: 30px; overflow: hidden;"></a>

因此,我使用此代码来捕获元素
(请注意 find_all 参数 a.icon.rest )

So I use this code to catch the element
(note the find_all argument a.icon.rest)

import requests
from bs4 import BeautifulSoup

url = 'http://www.zap.co.il/models.aspx?sog=e-cellphone&pageinfo=1'
source_code = requests.get(url)
plain_text = source_code.text
soup  = BeautifulSoup(plain_text, "html.parser")
for link in soup.find_all("a.icon.rest"):
    x = link.get('href')
    print(x)

不幸的是什么都没有返回
尽管漂亮的汤文档清楚地表明:

Which unfortunately returns nothing
although the beautiful soup documentation clearly says:

如果您要搜索与两个或多个CSS类匹配的标签,则可以 应该使用CSS选择器:

If you want to search for tags that match two or more CSS classes, you should use a CSS selector:

css_soup.select("p.strikeout.body")
returns: <p class="body strikeout"></p>

css_soup.select("p.strikeout.body")
returns: <p class="body strikeout"></p>

那么为什么这不起作用? 顺便说一句,我正在使用pycharm

So why isn't this working? By the way, I'm using pycharm

推荐答案

正如引用的文档所述,如果要搜索与两个CSS类匹配的标签,则必须使用CSS选择器而不是find_all .您引用的示例显示了如何执行此操作:

As the docs you quoted explain, if you want to search for tags that match two CSS classes, you have to use a CSS selector instead of a find_all. The example you quoted shows how to do that:

css_soup.select("p.strikeout.body")

但是你没有那样做;您仍然使用了find_all,但是它当然不起作用,因为find_all没有使用CSS选择器.

But you didn't do that; you used find_all anyway, and of course it didn't work, because find_all doesn't take a CSS selector.

将其更改为使用具有CSS选择器的select,它将起作用.

Change it to use select, which does take a CSS selector, and it will work.

这篇关于美丽的汤find_all找不到具有多个类的CSS选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆