-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (92 loc) · 3.64 KB
/
main.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
from selenium import webdriver
from time import sleep
from datetime import datetime
import json
import xlsxwriter
class VehicleBot:
def __init__(self):
# open config file
with open('config.json') as json_file:
self.config = json.load(json_file)
# open links file
with open('links.json') as json_file:
self.links = json.load(json_file)
# initialize web driver
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.vehicle_type = "bikes"
# for spreadsheet
self.row = 0
self.workbook = xlsxwriter.Workbook(
'C:/Users/Dan/Documents/Python Projects/VehicleBot/ads.xlsx')
self.worksheet = self.workbook.add_worksheet()
# loop through keywords
for keyword in self.config['keywords']:
for link in self.links[self.vehicle_type]:
# search in Ikman
self.search_ikman(link['ikman'], keyword)
sleep(2)
# search in Riyasewana
# self.search_riyasewana(link['riyasewana'], keyword)
sleep(2)
self.workbook.close()
exit()
def search_ikman(self, link, keyword):
self.driver.get(link)
sleep(2)
# Search by keyword
self.driver.find_element_by_xpath(
'//input[@type="search"]').send_keys(keyword)
self.driver.find_element_by_xpath(
'//button[contains(text(), "Search")]').click()
sleep(4)
# Get current url
currentURL = self.driver.current_url
# Get all ad links in page
resultSet = self.driver.find_element_by_xpath(
'//ul[contains(@class, "list--3NxGO")]')
ads = resultSet.find_elements_by_tag_name("li")
ad_links = []
for ad in ads:
ad_links.append(ad.find_element_by_tag_name(
"a").get_attribute('href'))
# Go through each ad
for ad_link in ad_links:
self.driver.get(ad_link)
# Get title
title = self.driver.find_element_by_xpath(
'//h1[contains(@class, "title--3s1R8")]').text
# Get price
price = self.driver.find_element_by_xpath(
'//div[contains(@class, "amount--3NTpl")]').text
# Get date, time, city and District
subTitle = self.driver.find_element_by_xpath(
'//span[contains(@class, "sub-title--37mkY")]').text
subTitle = subTitle.split(',')
dateTime = subTitle[0].split(' ')
dateTime.remove('Posted')
dateTime.remove('on')
dateTime = ' '.join(dateTime)
city = subTitle[1].strip()
district = subTitle[2].strip()
# Get model year
year = self.driver.find_element_by_xpath(
'//div[contains(text(), "Model year: ")]/following-sibling::div').text
# Get description
description = self.driver.find_element_by_xpath(
'//div[contains(@class, "description--1nRbz")]').text
ad_list = [title, price, year, city, district,
dateTime, description, ad_link]
print(
f"{year} - {title} - {price} - {city} - {district} - {dateTime} - {description} - {ad_link}")
self.write_sheet(ad_list, self.row)
self.row += 1
def search_riyasewana(self, link, keyword):
self.driver.get(link)
sleep(2)
def write_sheet(self, ad_list, row):
col = 0
for item in (ad_list):
self.worksheet.write(row, col, item)
col += 1
my_bot = VehicleBot()