Python爬取13个旅游城市,告诉你大家最爱去哪玩?

556次阅读
没有评论

Python爬取13个旅游城市,告诉你大家最爱去哪玩?

通过分析去哪儿网部分城市门票售卖情况,简单的分析一下哪些景点比较受欢迎。

用到的Python模块

BeautifulSoup、requests、pymongo、pylab

方法

通过请求https://piao.qunar.com/ticket/list.htm?keyword=北京  ,获取北京地区热门景区信息,再通过BeautifulSoup去分析提取出我们需要的信息。

这里为了偷懒只爬取了前4页的景点信息,每页有15个景点。因为去哪儿并没有什么反爬措施,所以直接请求就可以了。

这里只是随机选择了13个热门城市:北京, 上海, 成都, 三亚, 广州, 重庆, 深圳, 西安, 杭州, 厦门, 武汉, 大连, 苏州。

并将爬取的数据存到了MongoDB数据库 。

爬虫部分完整代码如下

import requests
from bs4 import BeautifulSoup
from pymongo import MongoClient
  
class QuNaEr():
    def __init__(self, keyword, page=1):
        self.keyword = keyword
        self.page = page
  
    def qne_spider(self):
        url = 'https://piao.qunar.com/ticket/list.htm?keyword=%s&region=&from=mpl_search_suggest&page=%s' % 
        (self.keyword, self.page)
        response = requests.get(url)
        response.encoding = 'utf-8'
        text = response.text
        bs_obj = BeautifulSoup(text, 'html.parser')
  
        arr = bs_obj.find('div', {'class': 'result_list'}).contents
        for i in arr:
            info = i.attrs
            # 景区名称
            name = info.get('data-sight-name')
            # 地址
            address = info.get('data-address')
            # 近期售票数
            count = info.get('data-sale-count')
            # 经纬度
            point = info.get('data-point')
  
            # 起始价格
            price = i.find('span', {'class': 'sight_item_price'})
            price = price.find_all('em')
            price = price[0].text
  
            conn = MongoClient('localhost', port=27017)
            db = conn.QuNaEr # 库
            table = db.qunaer_51 # 表
  
            table.insert_one({
                'name'      :   name,
                'address'   :   address,
                'count'     :   int(count),
                'point'     :   point,
                'price'     :   float(price),
                'city'      :   self.keyword
            })
  
if __name__ == '__main__':
    citys = ['北京', '上海', '成都', '三亚', '广州', '重庆', '深圳', '西安', '杭州', '厦门', '武汉', '大连', '苏州']
    for i in citys:
        for page in range(1, 5):
            qne = QuNaEr(i, page=page)
            qne.qne_spider()

有了数据,我们就可以分析出自己想要的东西了

最受欢迎的15个景区

Python爬取13个旅游城市,告诉你大家最爱去哪玩?

由图可以看出,在选择的13个城市中,最热门的景区为上海的迪士尼乐园

导入数据库,代码如下

from pymongo import MongoClient
# 设置字体,不然无法显示中文
from pylab import *
  
mpl.rcParams['font.sans-serif'] = ['SimHei']
  
conn = MongoClient('localhost', port=27017)
db = conn.QuNaEr # 库
table = db.qunaer_51 # 表
  
result = table.find().sort([('count', -1)]).limit(15)
# x,y轴数据
x_arr = []  # 景区名称
y_arr = []  # 销量
for i in result:
    x_arr.append(i['name'])
    y_arr.append(i['count'])
  
"""
去哪儿月销量排行榜
"""
plt.bar(x_arr, y_arr, color='rgb')  # 指定color,不然所有的柱体都会是一个颜色
plt.gcf().autofmt_xdate() # 旋转x轴,避免重叠
plt.xlabel(u'景点名称')  # x轴描述信息
plt.ylabel(u'月销量')  # y轴描述信息
plt.title(u'拉钩景点月销量统计表')  # 指定图表描述信息
plt.ylim(0, 4000)  # 指定Y轴的高度
plt.savefig('去哪儿月销售量排行榜')  # 保存为图片
plt.show()

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2020-01-23发表,共计3187字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)