【发布时间】:2015-09-09 08:40:44
【问题描述】:
我不明白为什么会出现这个错误:
我有一个相当简单的功能:
def scrape_a(url):
r = requests.get(url)
soup = BeautifulSoup(r.content)
news = soup.find_all("div", attrs={"class": "news"})
for links in news:
link = news.find_all("href")
return link
这是我要抓取的网页结构:
<div class="news">
<a href="www.link.com">
<h2 class="heading">
heading
</h2>
<div class="teaserImg">
<img alt="" border="0" height="124" src="/image">
</div>
<p> text </p>
</a>
</div>
【问题讨论】:
-
你为什么要遍历
news,然后调用news.find_all()?大概您打算改用links.find_all? -
另外,
href是标签的属性,而不是标签名。 -
另外,你的意思是只返回 first 结果吗?
标签: python web-scraping beautifulsoup