【发布时间】:2016-04-19 01:16:22
【问题描述】:
这是我要抓取的 html:
<span class="meta-attributes__attr-tags">
<a href="/tags/cinematic" title="cinematic">cinematic</a>,
<a href="/tags/dissolve" title="dissolve">dissolve</a>,
<a href="/tags/epic" title="epic">epic</a>,
<a href="/tags/fly" title="fly">fly</a>,
</span>
我想获取每个 a href 的锚文本:电影、溶解、史诗等。
这是我的代码:
url = urllib2.urlopen("http: example.com")
content = url.read()
soup = BeautifulSoup(content)
links = soup.find_all("span", {"class": "meta-attributes__attr-tags"})
for link in links:
print link.find_all('a')['href']
如果我使用“link.find_all”执行此操作,我会收到错误:TypeError: List indices must be integers, not str.
但如果我打印 link.find('a')['href'] 我只会得到第一个。
我怎样才能获得所有这些?
【问题讨论】:
标签: python beautifulsoup scrape