在美丽的汤上解析图形名称

时间:2017-05-23 15:19:16

标签: python web-scraping beautifulsoup

这是我第一次发帖,所以请保持温和。 我正在从旅行顾问那里提取数据。评论用图表来解释。

<span class="ui_bubble_rating bubble_40"></span>

正如你所看到的,最后有一个代表4颗星的“40”。 “20”(2星)等也是如此......

如何获取“ui_bubble_rating bubble_40”? 提前谢谢你......

2 个答案:

答案 0 :(得分:0)

我不确定这是否是最有效的方法,但这就是我的方法:

tags = soup.find_all(class=re.compile("bubble_\d\d"))

tags变量将包含页面中与正则表达式bubble_\d\d匹配的每个标记。之后,您只需要提取数字,如下所示:

stars = tags[0].split("_")[1]

如果您想要花哨,可以使用列表推导从每个标签中提取数字:

stars = [tag.split("_")[1] for tag in tags]

答案 1 :(得分:0)

我不确定你要抓什么样的数据, 但是你可以像这样获得那个span标签(我测试了它并留下了一些打印件):

from urllib import urlopen
from bs4 import BeautifulSoup
import re

html = urlopen("YOUR_REVIEWS_URL")
bs1=BeautifulSoup(html, 'lxml')

for s in bs1.findAll("span", {"class":"ui_bubble_rating bubble_40"}):
        print(s)

更通用的方式(刮掉所有评级(bubble_ [0-9] {2})):

toFind = re.compile("(bubble_[0-9]{2})+")
for s in bs1.findAll("span", {"class":toFind}):
        print(s)

希望能回答你的问题

相关问题