我試圖解析從具有屬性itemprop
使用BeautifulSoup4beautifulsoup4從特定屬性值的錨元素得到HREF
例如值url
一個頁面上的多個錨元素href值,從<a itemprop="url" href="/pages/page"></a>
提取/pages/page
但是在一個頁面中有多個這樣的項目,所以我想讓它們在一個數組中。
我想是這樣的soup("span", html = True, {'itemprop' : 'name' })
我試圖解析從具有屬性itemprop
使用BeautifulSoup4beautifulsoup4從特定屬性值的錨元素得到HREF
例如值url
一個頁面上的多個錨元素href值,從<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")]
你試過'find_all()'?顯示你的代碼。 – furas