-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairbnbscraper. py
150 lines (126 loc) · 6.72 KB
/
airbnbscraper. 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import csv
import parameters
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from parsel import Selector
writer = csv.writer(open("H://Bir.csv", 'w',encoding="utf-8"))
writer.writerow(['Name','Url','Location','Price','Description'])
driver=webdriver.Chrome('F:\\chromedriver')
driver.maximize_window()
sleep(2)
driver.get('https:\\www.airbnb.co.in')
sleep(2)
search_city = driver.find_element_by_xpath('//*[@class="_1f8ev6q"]').send_keys('Bir,Himachal pradesh \ue007')
sleep(3)
search_button=driver.find_element_by_xpath('//*[@type="submit"]').click()
sleep(2)
#stays= driver.find_element_by_xpath('//*[@class="_1dilvt2s"] | //a[text()="Stays"]').click()
#sleep(5)
cookies=driver.find_element_by_xpath('//*[@class="optanon-allow-all accept-cookies-button"]').click()
sleep(2)
#filters=driver.find_element_by_xpath('//*[@id="menuItemButton-dynamicMoreFilters"]').click()
#sleep(2)
#superhost=driver.find_element_by_xpath('//*[@id="filterItem-switch-superh9ost-true"]').click()
#sleep(2)
map=driver.find_element_by_xpath('//*[@class="_1i2wkdr"]').click()
#sleep(3)
#filters=driver.find_element_by_xpath('//*[@id="menuItemButton-dynamicMoreFilters"]').click()
#sleep(2)
#superhost=driver.find_element_by_xpath('//*[@id="filterItem-switch-superhost-true"]').click()
#sleep(2)9
#show_places=driver.find_element_by_xpath('//*[@class="_2i58o3a"]').click()
sleep(10)
home=driver.current_url
print(home)
for i in r9ange(1,300):
find_places=driver.find_elements_by_xpath('//*[@class="_1wz0grtk"]/a|'
'//*[@class="_1oajdzlk"]/a') #get the url for the place
find_places=[place.get_attribute('href') for place in find_places]
for place in find_places:
driver.get(place) #oprn in tab
sleep(7)
sel = Selector(text=driver.page_source)
Name=sel.xpath('//*[@itemprop="name"]//h1//text() |'
'//h1//text()[1]').extract() #extract name of location
Url=driver.current_url
Location=sel.xpath('//*[@class="_1hpgssa1"]//a//text() |'
'//*[@class="_ylytgbo"]//text()').extract() #get the url of the page
Price=sel.xpath('//span//*[@class="_doc79r"]//text()').extract() #get the price '//span//*[@class="_n4om66"]//text() |''//*[@class="_18ilrswp"]//text()'
Translate=sel.xpath('//*[@class="_1p3joamp"]//button[text="Translate"]')
if Translate:
Translate.click()
Description=sel.xpath('//*[@class="_6z3til"]//text() | '
'//*[@class="_11oyobo"]//text()').extract()
else:
Description=sel.xpath('//*[@class="_6z3til"]//text() | '
'//*[@class="_11oyobo"]//text()').extract()
print(Name) #print the variables
print(Url)
print(Location)
print(Price)
print(Description)
writer.writerow([Name,Url,Location,Price,Description])
driver.get(home)
while True:
try:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(2)
driver.refresh()
next_page=driver.find_element_by_xpath('//*[@class="_cfiftp"]//a[@aria-label="Next page"]')
next_page.click()
sleep(5)
print("----------------------------------------------------------------------------------")
#filters=driver.find_element_by_xpath('//*[@id="menuItemButton-dynamicMoreFilters"]').click()
#sleep(2)
#superhost=driver.find_element_by_xpath('//*[@id="filterItem-switch-superhost-true"]')
#sleep(2)
#show_places=driver.find_element_by_xpath('//*[@class="_2i58o3a"]').click()
#sleep(2)
#if superhost:
#check=driver.find_element_by_xpath('//*[@aria-checked="true"]')
#show_places=driver.find_element_by_xpath('//*[@class="_2i58o3a"]').click()
#sleep(5)
#else:
#superhost.click()
#show_places=driver.find_element_by_xpath('//*[@class="_2i58o3a"]').click()
#sleep(5)
next_page_url=driver.current_url
#driver.get(next_page_url)
print(next_page_url)
for i in range(1,300):
find_places=driver.find_elements_by_xpath('//*[@class="_1wz0grtk"]/a|'
'//*[@class="_1oajdzlk"]/a') #get the url for the place
find_places=[place.get_attribute('href') for place in find_places]
for place in find_places:
driver.get(place) #oprn in tab
sleep(7)
sel = Selector(text=driver.page_source)
Name=sel.xpath('//*[@itemprop="name"]//h1//text() |'
'//h1//text()[1]').extract() #extract name of location
Url=driver.current_url
Location=sel.xpath('//*[@class="_1hpgssa1"]//a//text() |'
'//*[@class="_ylytgbo"]//text()').extract() #get the url of the page
Price=sel.xpath('//span//*[@class="_doc79r"]//text()').extract() #get the price '//span//*[@class="_n4om66"]//text() |''//*[@class="_18ilrswp"]//text()'
Translate=sel.xpath('//*[@class="_1p3joamp"]//button[text="Translate"]')
if Translate:
Translate.click()
Description=sel.xpath('//*[@class="_6z3til"]//text() | '
'//*[@class="_11oyobo"]//text()').extract()
else:
Description=sel.xpath('//*[@class="_6z3til"]//text() | '
'//*[@class="_11oyobo"]//text()').extract()
print(Name) #print the variables
print(Url)
print(Location)
print(Price)
print(Description)
writer.writerow([Name,Url,Location,Price,Description])
driver.get(next_page_url)
except:
print('No more pages to load.')
sleep(5)
driver.close()
break
#