Python爬虫游全国:兰州站
本文是Python爬虫旅游全国的第7个城市:兰州,主要是介绍兰州的景点和美食信息。
之前写过厦门、长沙、西安、大连、苏州、成都等,感兴趣的可以阅读,也可以当做一份旅游指南~
兰州
摘录一段来自维基百科中关于兰州的简介:
兰州市,简称兰,别称金城,是中华人民共和国甘肃省省会,国务院批复确定的中国西北地区重要的工业基地和综合交通枢纽,丝绸之路经济带的重要节点城市,西部地区重要的中心城市之一,西北地区第三大城市,“兰州—西宁城市群”中的核心城市,位于甘肃省中部。
下面是兰州的行政区域:
爬取信息
爬取的信息主要是兰州的美食和景点信息:
1 | import pandas as pd |
景点爬取
下面单页爬取的代码,采用的是正则爬取的方式:
1 | url = "https://travel.qunar.com/p-cs300026-lanzhou-jingdian-1-1" |
如果解析的HTML源码中含有双引号,那么re.findall()方法后面的字符串的最外层使用单引号
In [3]:
1 | # 1-景点中文名称 |
Out[3]:
1 | ['甘肃省博物馆', |
In [4]:
1 |
|
Out[4]:
1 | ['Gansu Provincial Museum', |
In [5]:
1 | # 3-strategy攻略数量 |
Out[5]:
1 | ['50', '94', '40', '35', '0', '1', '3', '2', '0', '1'] |
In [6]:
1 | # 4-comment点评数量 |
Out[6]:
1 | ['1295', '2948', '793', '489', '23', '30', '46', '354', '146', '80'] |
In [7]:
1 | # 5-景点地点(长沙景点排名、宁乡景点排名等) |
Out[7]:
1 | ['兰州', '兰州', '兰州', '兰州', '永登', '兰州', '兰州', '兰州', '兰州', '兰州'] |
In [8]:
1 | # 6-景点排名 |
Out[8]:
1 | ['7', '3', '11', '10', '0%', '56', '1', '17', '5', '49'] |
In [9]:
1 | # 7-驴友 |
Out[9]:
1 | ['35%', '58%', '30%', '30%', '0%', '0%', '0%', '1%', '0%', '1%'] |
In [10]:
1 | # 8-景点简介 |
Out[10]:
1 | ['通过丰富精彩的展品了解古丝绸之路、唐蕃古道上多民族的文化和历史。', |
最终爬取的结果:
美食爬取
单页信息爬取的代码,同样是基于正则表达式的爬取:总共是200页
In [2]:
1 | url = "https://travel.qunar.com/p-cs300026-lanzhou-meishi?page=1" |
cn_title
In [3]:
1 | cn_title = re.findall('cn_tit">(.*?)</span>.*?countbox',result,re.S) |
In [4]:
1 | print(len(cn_title)) |
Out[4]:
1 | ['正宁路小吃夜市', |
score
In [5]:
1 | score = re.findall('cur_score">(.*?)</span>.*?total_score',result,re.S) |
In [6]:
1 | print(len(score)) |
Out[6]:
1 | ['4.3', '4.5', '--', '4.3', '3.5', '5.0', '4.2', '3.5', '4.2', '0.0'] |
sublistbox
先提取整个sublistbox,然后对里面的每个子元素单独提取
In [7]:
1 | sublistbox = re.findall('sublistbox">(.*?)</div>', result, re.S) |
Out[7]:
1 | ['<dl class="sublist_item clrfix"><dt class="sub_tit">人\u3000均</dt><dd class="sub_des">¥ 63</dd></dl><dl class="sublist_item clrfix"><dt class="sub_tit">地\u3000址</dt><dd class="sub_des des_line">白银路街道永昌南路正宁路</dd></dl><dl class="sublist_item clrfix"><dt class="sub_tit">推荐菜</dt><dd class="sub_des des_line">当地口味\t美食街\t老字号\t深夜营业</dd></dl><div class="desbox"><span class="img_doublequote img_l"></span><span class="txt">吃客云集的小吃街,地道吃食令人回味。<span class="img_doublequote img_r"></span></span>'] |
In [8]:
1 | type(sublistbox) |
Out[8]:
1 | list |
In [9]:
1 |
均价
In [10]:
1 | person_avg = [] |
In [11]:
1 | print(len(person_avg)) |
Out[11]:
1 | ['63', '27', 0, '19', '17', 0, '14', '58', '58', '18'] |
地址
In [12]:
1 | address = [] |
In [13]:
1 | print(len(address)) |
Out[13]:
1 | ['白银路街道永昌南路正宁路', |
推荐菜
In [14]:
1 | recommand = [] |
In [15]:
1 | print(len(recommand)) |
Out[15]:
1 | ['当地口味\t美食街\t老字号\t深夜营业', |
评价
In [16]:
1 | comment = [] |
In [17]:
1 | print(len(comment)) |
Out[17]:
1 | ['吃客云集的小吃街,地道吃食令人回味。', |
最终爬取结果:刚好是2000条数据
下面是对上面爬取到的两份数据进行分析:
美食数据分析
导入数据:
字段基本信息:
In [3]:
1 |
|
Out[3]:
1 | 中文名 0 |
In [4]:
1 |
|
Out[4]:
1 | 中文名 object |
分析哪些店的得分靠前:
In [5]:
1 | # 得分中有未评分的数据:-- |
Out[5]:
1 | -- 1721 |
In [6]:
1 | # 将未评分的数据统一替换成0.0,也就是0分 |
得分统计
大部分店铺的得分在3.5分,好像并不是很高~
得分前10的店铺
店铺均价占比
高档餐厅
面馆
到了兰州肯定得吃面:据数据统计有284家面馆
火锅店
统计出来有129家火锅店:
1 | px.bar(huoguo[:15],x="中文名",y="得分") |
美食统计信息
推荐菜词云
绘制当地推荐菜的词云图:
1 | rec_words = [tuple(z) for z in zip(result["词语"].tolist(), result["次数"].tolist())] |
景点数据分析
还是先导入数据
数据探索
In [3]:
1 | df1.shape |
Out[3]:
1 | (381, 8) |
In [4]:
1 | df1.isnull().sum() |
Out[4]:
1 | cn_title 0 |
在景点的英文名en_title和简介abstract中存在缺失值
In [5]:
1 | df1.dtypes |
Out[5]:
1 | cn_title object |
景点位置分布
In [6]:
1 | df2 = df1["location"].value_counts().reset_index() |
Out[6]:
location | number | |
---|---|---|
0 | 兰州 | 301 |
1 | 永登 | 39 |
2 | 皋兰 | 21 |
3 | 榆中 | 20 |
In [7]:
1 | c = ( |
攻略数
评论数前10名
1 | c = ( |
景点词云
前50个词语的展示:
1 | rec_words = [tuple(z) for z in zip(result["词语"].tolist(), result["次数"].tolist())] |
整体绘图
1 | # 1-景点位置 |