A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

今天,通过分析去哪儿网部分城市门票售卖情况,简单的分析一下哪些景点比较受欢迎。等下次假期可以做个参考。

用到的Python模块

BeautifulSoup、requests、pymongo、pylab

方法

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

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

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

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


爬虫部分完整代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

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://www.pzzs168.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 = https://www.aiidol.com/QuNaEr(i, page=page)
            qne.qne_spider()


效果图如下


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


最受欢迎的15个景区

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

代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

from pymongo import MongoClient
# 设置字体,不然无法显示中文
from pylab import *
  
mpl.rcParams['font.sans-serif'] = ['SimHei']
  
conn = https://www.idiancai.com/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()


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马