如何在webscraping中获取href

时间:2018-02-18 17:38:41

标签: python web-scraping beautifulsoup

 articles = contents.find_all('article')
 for article in articles:
     titles=article.find('div',{"class":"featured"})
     print(titles)

这将输出为

<div class="featured" style="background-image: url(https://therecipecritic.com/wp-content/uploads/2018/02/Mint-Oreo-Cheesecake-1-of-1-350x500.jpg)"> <a href="https://therecipecritic.com/2018/02/mint-oreo-cheesecake/" rel="bookmark"><span class="readpost">View the Recipe</span></a></div>

在这里我如何获得 a

元素的href

3 个答案:

答案 0 :(得分:0)

你很亲密。您可以通过访问查找href属性返回的结果的内置__getitem__方法来访问a

print(titles.find('a')['href'])

答案 1 :(得分:0)

您需要先访问@IBAction func didPinch(_ pinchGR: UIPinchGestureRecognizer) { self.view.layer.sublayers![0].setAffineTransform( self.view.layer.sublayers![0].affineTransform().scaledBy( x:pinchGR.scale, y:pinchGR.scale)) pinchGR.scale = 1.0; } 标记,然后像使用字典一样访问它的a属性:

href

答案 2 :(得分:0)

使用Python对猫进行皮肤修复的许多方法。我是这样做的......

wsHREF = wsArticle.find("<<tag>>", re.compile("<<classname>>.*")).attrs['href']

其中&#39; wsArticle&#39;是我的元素节点的BSoup对象,<<tag>>是要查找的HTML标记,<<classname>>是您要搜索的class,但我使用通配符查找以...开头的所有内容。 .. myclass ......等等。