### 构造映射字典
从刚才的页面解析中,我们已经明白了“\ueeb5”是3,“\ue753”是7,那么咱们再次在页面中找到其他的相关数字,【0-9】十个数字很容易就组合出来了。记住,1不需要重构,。
``` shell
self.woff = {
"\uf0b6": "0",
# "": "1",
"\uf802": "2",
"\ueeb5": "3",
"\ueb5e": "4",
"\uf508": "5",
"\ue867": "6",
"\ue753": "7",
"\uf0a6": "8",
"\uf506": "9",
}
不要正对着数据抄写,点评的字体svg经常改变。需要自己对应当时的情况写一下
```
省下的就是直接处理数据和解析数据了。
内容如下:
``` python
def get_count(self, uncode_list):
try:
count = ""
for uncode in uncode_list:
uncodes_ = uncode.replace('",
"").replace(
"", "").replace("", "").split('>')
# pprint.pprint(uncodes_)
for uncs in uncodes_:
if uncs in self.woff.keys():
cc = self.woff[uncs]
else:
cc = uncs
count += cc
return count
except Exception as e:
logger.info("数字解析出现错误")
return uncode_list[0]
def run(self):
url = 'http://www.dianping.com/shanghai/ch10/r801'
html = self.get_html(url)
shop_el_list = html.css("#shop-all-list li")
for shop_info in shop_el_list:
item = {}
shop_name = shop_info.css("div.txt div.tit a::attr(title) ").extract_first()
item["名称"] = shop_name
score = shop_info.css("div.txt div.comment div.nebula_star div.star_score::text").extract_first()
item["评分"] = score
review_num = shop_info.css("div.txt div.comment>a.review-num > b ").extract()
item["评价数"] = self.get_count(review_num)
mean_price = shop_info.css("div.txt div.comment>a.mean-price > b ").extract()
item["人均花费"]=self.get_count(mean_price)
# item["类型"]
# item["地址"]
recommend = shop_info.css("div.txt div.recommend>a::text").extract()
item["推荐"] = recommend
print(item)
```
上述代码就是这个demo中最关键的两个内容,一个是逻辑,一个是数字解析。这里我就不费口舌进行一一解释了。同学们自己看下吧。
### 成果展示
### 存在的缺点
-如果点评换了一版映射,咱们就要重新做一次字典集合
-只是简单的对【0-9】十个数字的映射做了解析。我想拿文字怎么办?
我的demo中就有类型和地址两个字段没有进行数据填充,因为这两个字段就是有文字映射的,这个问题会在下一篇中进行解答。
同时也会解决点评网站换动态字体该怎么解决的。
咱们Python爬虫丨大众点评数据爬虫教程(2)见~~~
本文章旨在用于交流分享,【未经允许,谢绝转载】