-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrent591-selenium.py
43 lines (33 loc) · 1.5 KB
/
rent591-selenium.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
import csv
from time import sleep
from parsel import Selector
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
writer = csv.writer(open("result_file.csv", 'w'))
writer.writerow(['type', 'area', 'floor', 'price', 'address', 'url'])
driver = webdriver.Chrome('/Users\jojo7\Documents\GitHub\scrapy_test\chromedriver')
driver.maximize_window()
sleep(0.5)
driver.get('https://rent.591.com.tw/?kind=0®ion=1&area=0,10&shape=2&other=balcony_1')
sleep(3)
driver.find_element_by_xpath('//a[@class="area-box-close"]').click()
sleep(3)
while True:
try:
sel = Selector(text=driver.page_source)
box = sel.xpath('//*[@class="listInfo clearfix "]')
for contents in box:
type = contents.xpath(".//p[1]/text()[1]").get().strip()
area = contents.xpath(".//p[1]/text()[2]").get().strip()
floor = contents.xpath(".//p[1]/text()[3]").get().strip()
price = contents.xpath('.//*[@class="price"]/i/text()').get()
url = contents.xpath('.//h3/a/@href').get()
address = contents.xpath('.//li[2]/p[2]/em/text()').get().strip()
writer.writerow([type, area, floor, price, address, url])
driver.find_element_by_xpath('//*[@class= "pageNext"]').click()
sleep(3)
except NoSuchElementException:
# logger.info('No more pages to load.')
driver.quit()
break