Fazlul

Untitled

Jun 3rd, 2021 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import scrapy
  2. from scrapy import Selector
  3. from scrapy_selenium import SeleniumRequest
  4. from selenium.common.exceptions import NoSuchElementException
  5.  
  6. class WinesSpider(scrapy.Spider):
  7. name = 'wine'
  8.  
  9. responses = []
  10.  
  11. def start_requests(self):
  12. yield SeleniumRequest(
  13. url='https://www.getwines.com/category_Wine',
  14. callback=self.parse
  15. )
  16.  
  17. def parse(self, response):
  18. driver = response.meta['driver']
  19. intial_page = driver.page_source
  20. self.responses.append(intial_page)
  21. found = True
  22. while found:
  23. try:
  24. next_page = driver.find_element_by_xpath("//a//b[text()= '>>' ")
  25. href = next_page.get_attribute('href')
  26. driver.click(href)
  27. driver.implicitly_wait(10)
  28. self.responses.append(driver.page_source)
  29.  
  30. except NoSuchElementException:
  31. break
  32.  
  33. for resp in self.responses:
  34. r = Selector(text=resp)
  35. products = r.xpath("(//div[@class='layMain']//tbody)[5]/tr")
  36. for product in products:
  37. yield {
  38. 'product_name':
  39. product.xpath(".//a[@class='Srch-producttitle']/text()").get()
  40. }
Add Comment
Please, Sign In to add comment