Python 使用beautifulsoup获取跨度标题

Python 使用beautifulsoup获取跨度标题,python,beautifulsoup,html,Python,Beautifulsoup,Html,我有这个跨度,我想用beautifulsoup把它缩短7分钟 <span>In current traffic: 7 mins</span> 但不起作用 *编辑 我的账号在下面 from bs4 import BeautifulSoup import urllib2 url = "https://maps.google.com.au/maps?saddr=A6&daddr=A6&hl=en&ll=-33.877613,151.039867&a

我有这个跨度,我想用beautifulsoup把它缩短7分钟

<span>In current traffic: 7 mins</span>
但不起作用

*编辑

我的账号在下面

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

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

使用字符串方法获取所需的零件:

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

res[…]
语法将允许您访问标记上的HTML属性,但是
根本没有属性。

您已经收到了它:

>>> res = soup.find('span')
>>> res
<span>In current traffic: 7 mins</span>
>>> 
要查找所需零件,可以使用“查找”:

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
编辑:更新我的代码以使用您的链接。

希望这有帮助

抱歉,Martijn的答案完全回答了问题。抱歉,但它不起作用,请在我的编辑上方查找。我是从一个有很多跨度的网站上得到这个消息的,很抱歉edit7min是一个动态值我感觉自己像一个noob,我得到了下面的错误文件“traffic.py”,第13行,打印为“Current Listeners:”,res.text[pos+2:]NameError:name“res”没有定义非常抱歉,这是我的错误,现已修复。它应该是
span.text
,而不是
res.text
。哈哈哈
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