我有这个范围,我想使用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
答案 0 :(得分: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
修改:更新了我的代码以使用您的链接 希望这有帮助!
答案 1 :(得分:1)
res
是带有文字的<span>
标记。你不能让BeautifulSoup进一步分解该文本,整个文本是一个单位:
>>> res.text
u'In current traffic: 7 mins'
使用字符串方法获取所需的部分:
>>> res.text.rsplit(':', 1)[-1].strip()
'7 mins'
res[...]
语法可让您访问标记上的HTML属性,但<span>
根本没有属性。