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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM