-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes_spider_plcity.py
64 lines (49 loc) · 2.47 KB
/
quotes_spider_plcity.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
import scrapy
import ipdb
import os
import time
import csv
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
class QuotesSpider(scrapy.Spider):
name = "plcity"
start_urls = [
]
def __init__(self, partnumber, *args, **kwargs):
self.start_urls = ['https://www.plc-city.com/shop/en/content/search?q=%s' % partnumber]
fireFoxOptions = Options()
fireFoxOptions.add_argument("--headless")
fireFoxOptions.add_argument("--window-size=1920,1080")
fireFoxOptions.add_argument('--start-maximized')
fireFoxOptions.add_argument('--disable-gpu')
fireFoxOptions.add_argument('--no-sandbox')
self.driver = webdriver.Firefox(options=fireFoxOptions, executable_path=r'../../geckodriver')
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(url=url, callback=self.parse_search_result)
def parse_search_result(self, response):
self.driver.get(response.url)
time.sleep(5)
item = self.driver.find_element(By.CSS_SELECTOR, "#sniperfast_search .sniperfast_product a")
links = [item.get_attribute('href')]
for link in links:
yield scrapy.Request(url=link, callback=self.parse_product_page)
def parse_product_page(self, response):
fireFoxOptions = Options()
fireFoxOptions.add_argument("--headless")
fireFoxOptions.add_argument("--window-size=1920,1080")
fireFoxOptions.add_argument('--start-maximized')
fireFoxOptions.add_argument('--disable-gpu')
fireFoxOptions.add_argument('--no-sandbox')
driver2 = webdriver.Firefox(options=fireFoxOptions, executable_path=r'../../geckodriver')
driver2.get(response.url)
name = driver2.find_element(By.XPATH, "//h1[contains(@itemprop,'name')]")
inventoryelement = driver2.find_element(By.XPATH, "//div[contains(@class,'avilability_status')]")
text = inventoryelement.text.rsplit(" ")
inventory_amount = text[1]
inventory_availability = text[0]
price = driver2.find_element(By.XPATH, "//span[contains(@id,'new_price_display')]").text
data = {'Distributor': 'plc-city', 'SKU': name.text, 'Inventory': inventory_availability, 'quantity': 1, 'price': price, 'ClickUrl': response.url, 'currency': 'USD'}
print (data)