我有repeates本身與其他的href鏈接幾次下面的HTML部分:Python 3中,美麗的湯,得到一個標籤
<div class="product-list-item margin-bottom">
<a title="titleexample" href="http://www.urlexample.com/example_1" data-style-id="sp_2866">
現在我想要得到這個文件是直接在所有的HREF鏈接在類別爲「product-list-item」的div標籤之後。 對於美麗的女孩來說,這是一件很新的事情,而且我沒想過工作。
感謝您的想法。
編輯:並不一定是beautifulsoup;當它可以用正則表達式和python html解析器完成時,這也可以。
EDIT2:我試了一下(我是很新的蟒蛇,所以我做了什麼可能是從一個先進的觀點完全以愚蠢):
soup = bs4.BeautifulSoup(htmlsource)
x = soup.find_all("div")
for i in range(len(x)):
if x[i].get("class") and "product-list-item" in x[i].get("class"):
print(x[i].get("class"))
這會給我所有「的產品名單-list項目」但後來我想是這樣
print(x[i].get("class").next_element)
因爲我想next_element或NEXT_SIBLING應該給我一個標籤,但它只是導致AttributeError的:‘名單’對象有沒有屬性‘next_element’。所以,我想,只有第一個列表元素:
print(x[i][0].get("class").next_element)
而導致這個錯誤:返回self.attrs [關鍵] KeyError異常:0 還與.find_all試過(「HREF」),並獲得(」 href「),但這一切都導致相同的錯誤。
EDIT3:好吧,似乎我發現瞭如何解決這個問題,現在我所做的:
x = soup.find_all("div", "product-list-item")
for i in x:
print(i.next_element.next_element.get("href"))
問候
:x = soup.find_all("div")
for i in range(len(x)):
if x[i].get("class") and "product-list-item" in x[i].get("class"):
print(x[i].next_element.next_element.get("href"))
這也可以通過其他屬性的find_all功能可縮短
你能告訴我們你試過什麼嗎?謝謝 – Drewdin