python 爬虫 booking爬取酒店信息

import requests
from bs4 import BeautifulSoup as bs
import re
import time
import pandas as pd


headers ={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
"Cookie":"设置好地区 货币等信息后的cookie"}

url_list=["用列表 好让爬虫自动遍历列表翻页。这里输入的url是在页面上选好地区 货币等信息后的url 这点一定注意!"]
info =[]
for ii in url_list:
time.sleep(2)
response = requests.get(ii,headers=headers)
#print(response.text)

#提取单个酒店信息代码块
soup = bs(response.text,"lxml")
hotle_info = soup.select("#hotellist_inner div.sr_item_content.sr_item_content_slider_wrapper")
#print(len(hotle_info))
#print(hotle_info)

#提取下一页链接
try:
next_page = soup.select("#search_results_table div.bui-pagination.results-paging div.bui-pagination__nav ul li.bui-pagination__item.bui-pagination__next-arrow a")
s5 = r'href="(.*?)" title='
next_page_halfurl =re.compile(s5,re.S).findall(str(next_page))[0]
#print(next_page_halfurl)
except:
break


#组合完整链接 并且放入队列 让其持续翻页
next_page_allurl = "https://www.booking.com"+ next_page_halfurl
url_list.append(next_page_allurl)


for i in hotle_info:
#每一个酒店的价格
s1 = r'<b class=" ">\n¥(.*?)\n</b>'
price = re.compile(s1,re.S).findall(str(i))
#print("".join(price)) #去壳方法 "".join() 空气join(内容)
price_info = ("".join(price))

#酒店的链接
s2 = r'href="\n(.*?)" rel="noopener"'
hotle_link =re.compile(s2,re.S).findall(str(i))[0]
#print("https://www.booking.com" + hotle_link)
hotle_link_info =("https://www.booking.com" + hotle_link)

#酒店的名称
s3 = r'sr-hotel__name " data-et-click="\n">\n(.*?)\n</span>'
hotle_name = re.compile(s3,re.S).findall(str(i))
#print("".join(hotle_name))
hotle_name_info = ("".join(hotle_name))

#酒店的区域
s4 = r'title="旅友们喜爱\n(.*?)\n的理由'
hotle_quyu = re.compile(s4,re.S).findall(str(i))
#print("".join(hotle_quyu))
hotle_quyu_info =("".join(hotle_quyu))

hotle_info_info = []
hotle_info_info.append(price_info)
hotle_info_info.append(hotle_link_info)
hotle_info_info.append(hotle_name_info)
hotle_info_info.append(hotle_quyu_info)

info.append(hotle_info_info)
print(info)
#将信息制作成表格,便于查看对比
name = ["价格","链接","名称","区域"]
test = pd.DataFrame(columns=name,data=info)
test.to_csv("D:\酒店.csv")

转载于:https://www.cnblogs.com/cwkcwk/p/9576533.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值