• 去哪儿爬虫加数据分析可视化


    爬虫

    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()
    

    数据分析可视化代码

    from pymongo import MongoClient
    from matplotlib import pyplot as plt
    import platform
    
    # 根据不同的平台设置字体,不然无法显示中文windows
    platform_dic = {"Darwin": "Arial Unicode MS", "Windows": "SimHei"}
    plt.rcParams['font.family'] = [platform_dic.get(platform.system())]
    plt.rcParams["axes.labelsize"] = 20  # axes是轴字体大小调整
    plt.rcParams["xtick.labelsize"] = 18  # 横坐标字体大小调整
    plt.rcParams["ytick.labelsize"] = 16  # 纵坐标字体大小调整
    plt.rcParams["figure.figsize"] = [20, 15]  # 显示图像的最大范围
    
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    conn = MongoClient('localhost', port=27017)
    db = conn.QuNaEr  # 库
    table = db.qunaer_51  # 表
    
    result = table.find().sort([('count', -1)]).limit(25)
    # 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('景点名称')  # x轴描述信息
    plt.ylabel('月销量')  # y轴描述信息
    plt.title('拉钩景点月销量统计表')  # 指定图表描述信息
    plt.ylim(0, 4000)  # 指定Y轴的高度
    plt.savefig('去哪儿月销售量排行榜')  # 保存为图片
    plt.show()
    
    
  • 相关阅读:
    编程练习--判断两个数组中数据的数据类型是否相同
    JS 类型检测
    JS学习笔记 等于和包装对象
    HTML5经典案例学习-----新元素添加文档结构
    JZ2440存储管理器--SDRAM
    GPIO实验之c语言
    1- 裸机开发GPIO
    Java基础教程——数组
    Java基础教程——运算符
    结构化编程·图示
  • 原文地址:https://www.cnblogs.com/c-x-a/p/10818096.html
Copyright © 2020-2023  润新知