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 でそのテキストをこれ以上分割することはできません。テキスト全体が1 つの単位です。

>>> 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 に答える