【发布时间】:2017-08-14 19:55:43
【问题描述】:
这是我要抓取的 html 示例。
<a id="catalogEntry_img3677183" href="http://www.academy.com/shop/pdp/under-armour%E2%84%A2-mens-tide-chaser-short-sleeve-shirt#repChildCatid=4099002" title="Under Armour Men's Tide Chaser Short Sleeve Shirt" onclick="javascript:dlTrackProductGridClicks("109457178","Under Armour Men's Tide Chaser Short Sleeve Shirt","3677183");">
我想检索 href 属性的引号内的链接。这是我写的代码。
a_ids = page_soup.findAll("a")
for a in range(len(a_ids)):
output = a_ids[a]["href"]
print(output)
但是,我从这段代码中得到的结果包括一堆来自其他 a 标签的乱七八糟的东西,如下所示。
<a href="http://www.academy.com/shop/pdp/bcg-mens-turbo-mesh-short-sleeve-t-
shirt#repChildCatid=4190420" id="catalogEntry_img4181006"
onclick="javascript:dlTrackProductGridClicks("109409336","BCG
Men's Turbo Mesh Short Sleeve T-shirt","4181006");"
title="BCG Men's Turbo Mesh Short Sleeve T-shirt">
<img alt="BCG Men's Turbo Mesh Short Sleeve T-shirt" onerror="this.onerror=null;this.src='//content.academy.com/weblib/images/coming-
soon.jpg';" src="//assets.academy.com/mgen/12/10740412.jpg?is=500,500"/>
<div class="product-info-attributes">
<!-- BEGIN AYRPriceDisplay.jspf -->
<div class="z-pricing" id="offerPrice_4181006">
$9.99
</div>
我只想要 href 标记中的链接。如何定位我想要的特定链接?作为参考,我试图抓取的网址来自这里:http://www.academy.com/shop/browse/apparel/mens-apparel/mens-shirts--t-shirts
【问题讨论】:
标签: python html beautifulsoup