【发布时间】:2013-05-31 15:43:25
【问题描述】:
我有以下 html 部分,它与其他 href 链接重复多次:
<div class="product-list-item margin-bottom">
<a title="titleexample" href="http://www.urlexample.com/example_1" data-style-id="sp_2866">
现在我想获取此文档中所有直接在 div 标记之后的带有“product-list-item”类的 href 链接。 对beautifulsoup 很陌生,我想出的任何方法都没有。
感谢您的想法。
编辑:不一定非要是美丽的汤;当它可以用正则表达式和python html解析器完成时,这也可以。
EDIT2:我尝试了什么(我对 python 还很陌生,所以从高级的角度来看,我所做的可能是完全愚蠢的):
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"))
这会给我一个所有“产品列表项”的列表,但后来我尝试了类似的东西
print(x[i].get("class").next_element)
因为我认为 next_element 或 next_sibling 应该给我下一个标签,但它只会导致 AttributeError: 'list' object has no attribute 'next_element'。所以我只尝试了第一个列表元素:
print(x[i][0].get("class").next_element)
导致此错误的原因:return self.attrs[key] KeyError: 0。 也尝试使用 .find_all("href") 和 .get("href") 但这都会导致相同的错误。
EDIT3:好吧,我似乎找到了解决方法,现在我做到了:
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 函数的另一个属性来缩短:
x = soup.find_all("div", "product-list-item")
for i in x:
print(i.next_element.next_element.get("href"))
问候
【问题讨论】:
-
你能告诉我们你尝试了什么吗?谢谢
标签: python python-3.x beautifulsoup