【发布时间】:2014-05-11 17:07:42
【问题描述】:
我正在从特定网站抓取固定内容。内容位于嵌套的 div 中,如下所示:
<div class="table-info">
<div>
<span>Time</span>
<div class="overflow-hidden">
<strong>Full</strong>
</div>
</div>
<div>
<span>Branch</span>
<div class="overflow-hidden">
<strong>IT</strong>
</div>
</div>
<div>
<span>Type</span>
<div class="overflow-hidden">
<strong>Standard</strong>
</div>
</div>
<div>
<span>contact</span>
<div class="overflow-hidden">
<strong>my location</strong>
</div>
</div>
</div>
我想用字符串值 Branch 检索 span 内 div 'overflow-hidden' 中唯一的 strong 内容。我使用的代码是:
from bs4 import BeautifulSoup
import urllib2
url = urllib2.urlopen("https://www.xyz.com")
content = url.read()
soup = BeautifulSoup(content)
type = soup.find('div',attrs={"class":"table-info"}).findAll('span')
print type
我已经抓取了主 div 'table-info' 中的所有 span 内容,以便我可以使用条件语句来检索所需的内容。但是,如果我尝试将跨度内的 div 内容废弃为:
type = soup.find('div',attrs={"class":"table-info"}).findAll('span').find('div')
print type
我得到错误:
AttributeError: 'list' object has no attribute 'find'
谁能给我一些想法来检索跨度中 div 的内容。谢谢你。 我正在使用python2.7
【问题讨论】:
标签: python html web-scraping beautifulsoup