【发布时间】:2014-03-24 16:44:50
【问题描述】:
我是 Python 新手(我只知道 powershell),我正在尝试使用 BS4+Python3 学习 Web Crawling。
这是我正在练习的一个简单练习:
<h1 class="entry-title">
<a href="test1.html">test1</a></h1>
<h1 class="entry-title">
<a href="test2.html" rel="bookmark">test2</a></h1>
我想要做的是只获取具有“rel”属性的详细信息(href 和 .string)
这是我的代码
for h1_Tag in soup.find_all(("h1", { "class" : "entry-title" })):
for a_Tag in h1_Tag.contents:
if a_Tag.has_attr('rel'):
print (a_Tag)
但我得到: AttributeError:“NavigableString”对象没有属性“has_attr”
我做错了什么? 任何帮助表示赞赏。
谢谢!
【问题讨论】:
标签: python python-3.x beautifulsoup