![](/img/trans.png)
[英]BeautifulSoup4: Fail to find 'a' tag with specific href value by find()
[英]beautifulsoup4 get href from anchor element with specific attribute value
我試圖解析從具有屬性的頁面上的多個錨元素href值itemprop
與價值url
使用BeautifulSoup4
例如,從<a itemprop="url" href="/pages/page"></a>
提取/pages/page
,但在一個頁面中有多個這樣的項目,所以我希望它們在一個數組中。
我在想這樣的soup("span", html = True, {'itemprop' : 'name' })
通過find_all()
您可以在解析的輸出中搜索特定標記。 在你的情況下,這很容易。 如果第一個參數是一個字符串,它只會找到具有該名稱的標簽。 所以soup.find_all("a")
會找到所有錨標簽。
現在它還支持(幾乎)任何關鍵字參數,以進一步縮小您的選擇范圍。 在您的情況下,您希望將屬性itemprop
設置為url
以便您可以使用soup.find_all("a", itemprop="url")
。
現在,它將返回一個標記列表,如果要從這些標記中提取href
屬性,可以使用tag.get("href")
。 最終結果將是這樣的:
anchored_tags = [tag.get("href") for tag in soup.find_all("a", itemprop="url")]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.