如何仅从Webscraper打印URL数据

时间:2015-05-01 09:54:44

标签: python html scraper

我正在构建一个webscraper并希望它从标题中检索url。 这是我目前正在使用的代码:

for item in g_data: 
    print item.contents[1].find_all("a", {"class": "a-link-normal"})[1]

这打印:

<a class="a-link-normal s-access-detail-page a-text-normal"
 href="http://www.amazon.co.uk/Scotch-BUFF-Brown-Packaging-Parcel/dp/B001OYOI5E"
 title="3M Scotch BUFF Brown Packaging Parcel Tape 50mm x 66m - Pack of
 2"><h2 class="a-size-medium a-color-null s-inline s-access-title
 a-text-normal">3M Scotch BUFF Brown Packaging Parcel Tape 50mm x 66m -
 Pack of 2</h2></a>

现在我想要的是能够获得

"http://www.amazon.co.uk/Scotch-BUFF-Brown-Packaging-Parcel/dp/B001OYOI5E"

,但我不确定如何定位特定数据。 有谁知道怎么做?,我真的很感激,谢谢。

虽然与其他帖子类似,但这是不同的,并不复杂,我认为其他问题的解决方案可行,但需要重写代码。

1 个答案:

答案 0 :(得分:0)

您只需要href-attribute的值,而不是打印整个anchor元素。您可以按如下方式访问此属性:

for item in g_data: 
    print item.contents[1].find_all("a", {"class": "a-link-normal"})[1]['href']
相关问题