2

我有这个跨度,我想用 Beautifulsoup 把 7 分钟搞定?

<span>In current traffic: 7 mins</span>

我试过了

res = soup.find('span')
title = res['In current traffic']
print 'Current Listeners:', title.text  

但不起作用

*编辑

我的实际代码如下

from bs4 import BeautifulSoup
import urllib2


url = "https://maps.google.com.au/maps?saddr=A6&daddr=A6&hl=en&ll=-33.877613,151.039867&spn=0.081236,0.083599&sll=-33.869204,151.034546&sspn=0.081244,0.083599&geocode=FYSu-v0d2KMACQ%3BFbp0-_0dJKoACQ&mra=ls&t=m&z=14&layer=t"

content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content)

res = soup.find('span')
title = res['In current traffic']
print 'Current Listeners:', res.text
4

2 回答 2

4

您已经收到它:

>>> res = soup.find('span')
>>> res
<span>In current traffic: 7 mins</span>
>>> 

要访问数据,请检查res.text

>>> res.text
u'In current traffic: 7 mins'

要找到您想要的部分,您可以使用 find:

pos = res.text.find(': ')
res.text[pos+2:]

所以,你的完整代码应该是:

from bs4 import BeautifulSoup
import urllib2


url = "https://maps.google.com.au/maps?saddr=A6&daddr=A6&hl=en&ll=-33.877613,151.039867&spn=0.081236,0.083599&sll=-33.869204,151.034546&sspn=0.081244,0.083599&geocode=FYSu-v0d2KMACQ%3BFbp0-_0dJKoACQ&mra=ls&t=m&z=14&layer=t"

content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content)

div = soup.find('div', {'class':'altroute-rcol altroute-aux'}) #get the div where it's located
span = div.find('span')
pos = span.text.find(': ')
print 'Current Listeners:', span.text[pos+2:]

结果:

Current Listeners: 7 min

编辑:更新了我的代码以使用您的链接。
希望这可以帮助!

于 2013-10-31T11:27:51.603 回答
1

res<span>带有文本的标签。您不能让 BeautifulSoup 进一步分解该文本,整个文本是一个单元:

>>> res.text
u'In current traffic: 7 mins'

使用字符串方法获取您想要的部分:

>>> res.text.rsplit(':', 1)[-1].strip()
'7 mins'

res[...]语法可以让您访问标签上的 HTML 属性,但<span>根本没有属性。

于 2013-10-31T11:25:53.050 回答