爬取携程信息

# -*- coding: utf-8 -*-
"""
Created on Mon May  7 15:18:17 2018

@author: chens
"""

import urllib.request
from bs4 import BeautifulSoup
import pymysql.cursors
'''
目标:获取酒店名称和酒店星级
'''
url1 = 'http://hotels.ctrip.com/hotel/435383.html'

html1 = urllib.request.urlopen(url1).read().decode('utf-8')

#1.获取酒店名称信息
soup_name = BeautifulSoup(html1,'lxml')

hotelName = soup_name.h2.string
print("酒店名称为:{}".format(hotelName))


#2.获取酒店星级信息
result_star = soup_name.find_all(attrs={"class":"grade"})

#print(result1)
result_star =str(result_star)

soup_star = BeautifulSoup(result_star,'lxml')

result_star = soup_star.find_all('span')
hotelStar = result_star[0]['title']
print("酒店星级为:{}".format(hotelStar))

'''
目标:获取酒店最低房价和评论总数
'''
url2 = "http://m.ctrip.com/html5/hotel/HotelDetail/435383.html"

html2 = urllib.request.urlopen(url2).read().decode('utf-8')

soup_comment = BeautifulSoup(html2,'lxml')
#评论总数
result_comment = soup_comment.find_all(attrs={"class":"hd js_comment_title"})
result_comment = str(result_comment)
soup_comment = BeautifulSoup(result_comment,'lxml')
commentCounts = soup_comment.find_all('em')[1].string
print("评论总数为:{}".format(commentCounts))


'''
目标:获取酒店卫生评分、环境评分、服务评分、设施评分、用户推荐比、用户评分、评价内容
'''

url3 = 'http://m.ctrip.com/html5/hotel/HotelDetail/dianping/435383.html'

html3 = urllib.request.urlopen(url3).read().decode('utf-8')

soup_grades = BeautifulSoup(html3,'lxml')

#获取酒店各项评分数据
result_grades = soup_grades.find_all(attrs={"class":"ve-txt"})
result_grades = str(result_grades)
soup32 = BeautifulSoup(result_grades, 'lxml')

result33 = soup32.find_all('em')
userRecommendRate = result33[0].string
hRating = result33[1].string
eRating = result33[2].string
sRating = result33[3].string
iRating = result33[4].string

print("用户推荐为:{}".format(userRecommendRate))
print("卫生评分为:{}分".format(hRating))
print("环境评分为:{}分".format(eRating))
print("服务评分为:{}分".format(sRating))
print("设施评分为:{}分".format(iRating))


#提取用户评论数据



for i in range(0,10):
    
    result_pf = soup_grades.find_all(attrs={"class":"g-ve"})
    userRating = result_pf[i].string
    print("评分为:{}".format(userRating))
    
    
    #userName = result35[i].get_text()
    result_data = soup_grades.find_all(attrs={"class":"tree-ellips-line6"})
    commentText = result_data[i].get_text()
    print("评论内容为:{}".format(commentText))


'''
    数据库操作
'''

#获取数据库链接
connection  = pymysql.connect(host = 'localhost',
                          user = 'root',
                          password = 'root',
                          db = 'xiecheng',
                          charset = 'utf8mb4')
try:
    #获取会话指针
    with connection.cursor() as cursor:
        #创建sql语句
        sql = "insert into `macro-polo` (`hotelName`,`hotelStar`,`commentCounts`,`userRecommendRate`,`hRating`,`eRating`,`sRating`,`iRating`,`userRating`,`commentText`) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

        #执行sql语句
        cursor.execute(sql,(hotelName,hotelStar,commentCounts,userRecommendRate,hRating,eRating,sRating,iRating,userRating,commentText))

        #提交数据库
        connection.commit()
finally:
    connection.close()
酒店名称为:杭州马可波罗假日酒店
酒店星级为:国家旅游局评定为四星级
评论总数为:(21771条)
用户推荐为:98%推荐
卫生评分为:卫生 4.8分
环境评分为:环境 4.7分
服务评分为:服务 4.7分
设施评分为:设施 4.6分
评分为:4.8
评论内容为:
                                    自驾车方便,离西湖很近!就是杭州交通拥堵,一片混乱,交警管理好像不怎么得力,满大街乱按喇叭,必要时车可以直接开上人行道,停车困难!酒店设施还算可以,服务也不过不失,对比周边的酒店性价比算高,是下榻杭州不错的选择!如果不选择西湖边或者灵隐寺附近的五星酒店,这间是我入住杭州比较满意的酒店啦!

评分为:5.0
评论内容为:
                                    房间设施齐全,干净卫生,自助早餐非常好!平海路附近酒店马可波罗假日是最不起眼的,但房间设施卫生是最好的,性价比超高。离西湖边和机场大巴站非常近。

评分为:5.0
评论内容为:
                                    升级了亲子房,两床的,我和妈妈觉得床稍微硬了点,其他都挺好,位置方便,干净卫生,房间又特别大,我想假如带上女儿,她肯定会特别喜欢她的小房间,很多玩具,酒店很用心,可以放心入住,出来旅游八天,体验最好的酒店。位置交通方便

评分为:4.0
评论内容为:
                                    酒店地处闹中心,出入方便,不远处就西湖景区,去各景区旅游都便行,不远庆春路103路去梅家坞方便,酒店装修时间不长很好,房内设备齐全,性价比很高,也很干净。不足之处地毯较破旧。希望能改善。谢谢!

评分为:4.0
评论内容为:
                                    酒店位置优越离西湖很近步行三分钟到五分钟就可以到达,我住的房间窗户也可以看到西湖,整体感觉很不错,出门打车也很方便!

评分为:4.8
评论内容为:
                                    房间很干净,服务挺好,早餐有点贵,晚上睡觉空调很热,下次要问服务员要个冷器扇之类的。基本上都很满意。

评分为:5.0
评论内容为:
                                    第一次选择这家酒店入住,以前每次都住华侨的,感觉华侨除了就在西湖边上这个优势,其他的越来越差了,设施越来越陈旧还有早餐也没有以前丰富,进入这家的客房真的是眼前一亮,虽然比较小巧但是五脏俱全,非常的人性化,房间收拾的很干净,最主要的是地理位置很好,到哪儿都很方便,离地铁龙翔桥站、西湖都非常的近,说完它的好再提几个小意见,房间的通风不是很好,不开空调大冬天的还是很闷热,窗子只能开条缝,卫生间更不用说了,热气根本没法排出去,还有就是卫生纸的质量太差了,又薄又容易破,还好自备着的,否则就尴尬了,以上两点若能改善一下那就完美了,总的来说是一家性价比比较高的酒店,以后来杭州还会光顾的。

评分为:5.0
评论内容为:
                                    虽然不是很新的酒店,但是出差到杭州都会住这间。地段很方便,走路到西湖只要一刻钟,到地铁更近。吃饭,游玩都不错。酒店服务有人情味,房间里居然准备了两双大拖鞋加一双儿童拖鞋,有大把雨伞。茶杯被放在了消毒柜里,加装了智能马桶盖。茶包也很用心,龙井绿茶加祁门红茶,早餐出品不错。

评分为:5.0
评论内容为:
                                    环境非常好,看图就知道了。最赞的是:地点最佳,就在西湖边上,步行5分钟非常方便哦……还有他的早餐 质量不错的。而且早餐厅的装修风格很棒哦,极力推荐

评分为:5.0
评论内容为:
                                    中心位置,靠近医院,来杭州好居处。环境设施齐全,房间卫生间里添加了体验智能全自动马桶,真心不错。这次特殊原因,所以更加有种宾至如归的感受,心情非常愉悦。期待下次再次光临马可、波罗假日酒店。

猜你喜欢

转载自blog.csdn.net/qq_34000894/article/details/80228491
今日推荐