如何在Python中从<span class =“ className”>我想要的文本</ span>中获取文本

use*_*716 3 python urllib2 beautifulsoup web-scraping

如何根据标题获得文本,根据标题获得21,427以下屏幕截图。

我尝试了一下,但没有成功:

rating_count = soup.find("span", attrs={'class':'rating_count'})
print rating_count
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这是输出

在此处输入图片说明

Dav*_*d A 5

这将完全满足您的需求。

from BeautifulSoup import BeautifulSoup

data='<span class="rating-count">TEXT I WANT</span>'
soup=BeautifulSoup(data)
t=soup.find('span',{'class':'rating-count'})
print t.text
Run Code Online (Sandbox Code Playgroud)

编辑:

根据您提供的代码。好像没有定义标题,因此Google不会发送您要查找的信息。因此,BeautifulSoup无法找到跨度,因为它实际上不存在。试试这个,对我有用:

pkg = "com.mavdev.focusoutfacebook"
url = "https://play.google.com/store/apps/details?id=" + pkg
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
data = opener.open(url).read()

soup=BeautifulSoup(data)

t=soup.find('span',{'class':'rating-count'})
print t.text
Run Code Online (Sandbox Code Playgroud)

结果:

>>> 
1,397
Run Code Online (Sandbox Code Playgroud)