我正在尝试解析以下HTML:
<div class="product-details">
<h4 class="title" ><a href="https://productwebpage.com/blue-standard" title="Blue - Standard">Blue - Standard</a></h4>
<a class="learn-more" data-test-selector="linkViewMoreDetails" href="https://productwebpage.com">Learn More</a>
<div class="tocart" <a class="" href="/store/addtocartplp?productId=3593" id="AddToCartSimple-3593">Add To Cart</a></div>
</div>
,
<div class="product-details">
<h4 class="title" ><a href="https://productwebpage.com/blue-wide" title="Blue - Wide">Blue - Wide</a></h4>
<a class="learn-more" data-test-selector="linkViewMoreDetails" href="https://productwebpage.com">Learn More</a>
<div class="tocart" <a class="disAddtoCardBtn" href="javascript:void(0)" id="AddToCartSimple-3576" >SOLD</a></div>
</div>
我正在尝试获取标题包含“蓝色”且商品未售出的“id”
我试过:
soup.find_all("a",href=re.compile("Blue"),class_="")
links = soup.find_all("a", href=re.compile("Blue", "Add To Cart"))
ids = [tag["id"] for tag in soup.find_all("a", href=re.compile("Blue"))]
但它并没有返回我想要的信息
我希望它能返回:
AddToCartSimple-3593
我认为你的html被破坏了。您可以使用
:has
、:not
和:contains
(:-soup-contains
-latest soupsive)以及属性=值选择器使用css选择器进行整个筛选。^是一个以开头的运算符,表示属性值以=。~是一般同级组合子,而>;是一个儿童组合器。这意味着寻找一个类为(.)tocart
的同级,然后是一个id以AddToCartSimple-
开头但没有显示包含SOLD
的文本的子级。不如!="SOLD"
具体,因为它可以是部分字符串排除。取决于实际数据中观察到的变化当然,在使用['id']访问之前,您应该检查是否存在匹配项。您还可以按如下方式进行所有匹配:
相关问题 更多 >
编程相关推荐