透過昨天的輸入資料,我們就可以來把booking上的資料都拿到手啦
driver = webdriver.Edge()
driver.get(f"https://www.booking.com/searchresults.zh-tw.html?ss={ss}&checkin={checkin}&checkout={checkout}&group_adults={adults}&no_rooms={room}&group_children={children}&order=price")
content = driver.page_source
soup = BeautifulSoup(content, "html.parser")
hotels = soup.find_all('div',attrs={"data-testid" : "property-card-container"})
# link_list = [] #飯店連結
driver.get("https://www.google.com.tw/maps/")
for i in range(5):
title = hotels[i].find(attrs={"data-testid" : "title"})
price = hotels[i].find(attrs={"data-testid" : "price-and-discounted-price"})
distence = hotels[i].find(attrs={"data-testid" : "distance"})
link = hotels[i].find(attrs={"data-testid" : "title-link"})
link_list.append(link.get('href'))
print(title.text)
rooms = hotels[i].find_all(attrs={"class" : "c59cd18527"}) #房間區塊
for rm in rooms:
rooms_type = rm.find_all(attrs={"class" : "e8f7c070a7"}) #房型區塊
for rmtp in rooms_type:
rooms_bedtype = rm.find_all(attrs={"class" : "fc367255e6"}) #床型區塊
print(rmtp.text)#房型
for bdtp in rooms_bedtype:
for bd in bdtp:
print(' '+bd.text) #床型
print(distence.text+' '+price.text) #距離+價錢
await ctx.send('--------------------------------------------------')
下面舉個例子
print(title.text)
await ctx.send(title.text)
把全部的程式碼改完後就長這個樣子了
await ctx.send(title.text #飯店名稱
rooms = hotels[i].find_all(attrs={"class" : "c59cd18527"}) #房間區塊
for rm in rooms:
rooms_type = rm.find_all(attrs={"class" : "e8f7c070a7"}) #房型區塊
for rmtp in rooms_type:
rooms_bedtype = rm.find_all(attrs={"class" : "fc367255e6"}) #床型區塊
await ctx.send(rmtp.text) #房型
for bdtp in rooms_bedtype:
for bd in bdtp:
await ctx.send(' '+bd.text) #床型
await ctx.send(distence.text+' '+price.text) #距離
await ctx.send('--------------------------------------------------')
讓我們看看成果如何