创新项目实训(四)

创新项目实训(四)

前言

我们组打算搭建一个国内旅游比价网站,
而我负责的部份是各大订酒店网站的数据获取及整理

主要参考版上的经验分享+自己的修改理解
小白0经验入门记录、边爬边学习ing
有错误或更好的建议都可以指教讨论

缤客Booking.com

采用python+request+beautifulsoup
目标获取酒店名称、星级、用户评分、评论数、最低价格

上个结果图
阳春的展示结果

在这里插入图片描述

正片开始

搜索上海的酒店,不用登入就能看到价格

url删减后可以剩以下参数
https://www.booking.com/searchresults.zh-cn.html?checkin_month={入住月份}&checkin_monthday={入住日期}&checkin_year={入住年份}&checkout_month={退房月份}&checkout_monthday={退房日期}&checkout_year={退房年份}&group_adults={大人数}&group_children={小孩数}&no_rooms={房间数}&ss={城市名}

在这里插入图片描述

翻到第二页,可以看到后面多了row&offset在这里插入图片描述
再翻到第三页,可以看到offset变成50
在这里插入图片描述
所以可以靠更改offset获取每页的数据


ok开始写代码

先获取每页的资料

def getdata(city,checkin,checkout,start):
    url = 'https://www.booking.com/searchresults.zh-cn.html?'
    header ={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
        }
    param ={
        'ss': city,
        'checkin_monthday': checkin[8:10],
        'checkin_year_month': checkin[0:7],
        'checkout_monthday': checkout[8:10],
        'checkout_year_month': checkout[0:7],
        'group_adults': 2,
        'group_children': 0,
        'no_rooms': 1,
    }
	#第一页
    if start==0:
        res = requests.get(url=url, headers=header, params=param)
        total_item = "".join(re.findall(r'b\_available\_hotels\:(.*?)\,', res.text))
        print('酒店总数',total_item)
        page = math.ceil(int(total_item) / 25)
        return res.text,page
    else:
        param['offset'] =start
        res = requests.get(url=url, headers=header, params=param)
        return res.text

再来用bs4来提取我们要的数据,现确认下元素在哪里

在这里插入图片描述

接下来直接提取就可以了

def printList(list,day):
    soup = BeautifulSoup(list, 'html.parser')
    Hotels = soup.select('.sr_property_block')

    for name in Hotels:
        text = name.select('.sr-hotel__name')[0].get_text().strip() + name.select('.bui-review-score__badge')[
            0].get_text().strip()
        str = 'https://www.booking.com/' + name.select('.hotel_name_link')[0]['href'].strip()
        print('----------------------------------------')
        print(text)
        print(name.select('.hotel_image')[0]['data-highres'])
        print(str.rstrip(';highlight_room=#hotelTmpl').strip())
        print(name.select('.bui-review-score__text')[0].get_text().strip())
        price =int(int(name.select('.bui-price-display__value')[0].get_text().strip().strip('元').replace(',',''))/day)
        print('CNY',price)

但星级的部份,因为不是每间酒店都有星级,用beautifulsoup获取的话匹配到,还在想有没有其他办法能解决

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值