簡體   English   中英

漂亮的湯find_all找不到具有多個類的CSS選擇器

[英]Beautiful soup find_all doesn't find CSS selector with multiple classes

網站上有這個<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

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)

不幸的是哪一個都不返回
盡管漂亮的湯文檔清楚地表明:

如果要搜索與兩個或多個CSS類匹配的標簽,則應使用CSS選擇器:

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

那為什么不起作用呢? 順便說一句,我正在使用pycharm

如您引用的文檔所述,如果要搜索與兩個CSS類匹配的標簽,則必須使用CSS選擇器而不是find_all 您引用的示例顯示了如何執行此操作:

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

但是你沒有那樣做。 您仍然使用了find_all ,當然它沒有用,因為find_all沒有使用CSS選擇器。

將其更改為使用select ,它確實帶有CSS選擇器,並且可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM