-
Notifications
You must be signed in to change notification settings - Fork 11
/
url_parser_py3.py
62 lines (53 loc) · 2.43 KB
/
url_parser_py3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from selenium import webdriver
import requests
import time
import csv
import re
options = webdriver.ChromeOptions()
options.add_argument('headless')
target_url = 'https://www.tripadvisor.com.au/Hotels-g255055-Australia-Hotels.html'
driver = webdriver.Chrome('./chromedriver', chrome_options=options)
driver.get(target_url)
driver.maximize_window()
soup = BeautifulSoup(driver.page_source, 'html.parser')
domain = 'https://www.tripadvisor.com.au'
# scrape page
next_page = '//*[@id="taplc_main_pagination_bar_dusty_hotels_resp_0"]/div/div/div/span[2]'
page_down = "window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;"
page_list = range(int(soup.find("div", {"class": "pageNumbers"}).find_all("a")[-1].get("data-page-number")))
print("Total number of page: {}".format(len(page_list)))
with open('./data/url_parser.csv', 'a') as csvfile:
fieldnames = ['hotel_id', 'hotel_name', 'n_comment', 'n_star', 'url']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
index = 0
for p in page_list:
print('the number of page = {0}/{1}'.format(p+1, len(page_list)))
soup = BeautifulSoup(driver.page_source, 'html.parser')
hotel_blocks = soup.find_all('div', {"class": "prw_rup prw_meta_hsx_responsive_listing ui_section listItem"})
for element in hotel_blocks:
index += 1
hotel_name = element.find('div', {"class": "listing_title"}).text
url = domain+element.find('div', {"class": "listing_title"}).find('a').get('href')
n_comment = element.find('a', {"class": "review_count"}).text
n_comment = re.sub('[^0-9,]', "", n_comment).replace(',','')
n_star = element.find('a', {"data-clicksource":"BubbleRating"}).attrs['alt']
writer.writerow(
{
'hotel_id':index,
'hotel_name':hotel_name,
'n_comment':n_comment,
'n_star':n_star,
'url':url
}
)
try:
driver.execute_script(page_down)
time.sleep(5)
driver.find_element_by_xpath(next_page).click()
time.sleep(8)
except:
print('in the end')
driver.quit()