Advertisement
Guest User

Untitled

a guest
May 14th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. import scrapy
  2. import pymysql.cursors
  3.  
  4. # Connect to the database
  5. connection = pymysql.connect(host='localhost',
  6. user='root',
  7. password='Kradz579032!!',
  8. db='db',
  9. charset='utf8mb4',
  10. cursorclass=pymysql.cursors.DictCursor)
  11.  
  12. class QuotesSpider(scrapy.Spider):
  13. name = "quotes"
  14.  
  15. def start_requests(self):
  16. urls = [
  17. 'https://www.banggood.com/Wholesale-RC-Helicopter-c-264.html',
  18. 'https://www.banggood.com/Wholesale-RC-Quadcopters-c-1848.html',
  19. ]
  20. for url in urls:
  21. yield scrapy.Request(url=url, callback=self.parse)
  22.  
  23. def parse(self, response):
  24. for quote in response.css('.good_box_min'):
  25. yield {
  26. 'producttitle': response.css('a.middle_product_text_170717::text').extract(),
  27. 'productlink': response.css('a.middle_product_text_170717::attr(href)').extract(),
  28. 'productprice': response.css('span.price.wh_cn::text').extract(),
  29. #'text': quote.css('span.text::text').extract_first(),
  30. #'author': quote.css('small.author::text').extract_first(),
  31. #'tags': quote.css('div.tags a.tag::text').extract(),
  32. }
  33. next_page = response.css('a#listNextPage::attr(href)').extract_first()
  34. if next_page is not None:
  35. next_page = response.urljoin(next_page)
  36. yield scrapy.Request(next_page, callback=self.parse)
  37.  
  38.  
  39.  
  40. try:
  41. with connection.cursor() as cursor:
  42.  
  43. sql = "INSERT INTO `banggood` (`producttitle`, `productlink`, `productprice`) VALUES (%s, %s, %s)"
  44. cursor.execute(sql, ('title', 'productlink', 'productprice'))
  45.  
  46.  
  47. connection.commit()
  48.  
  49.  
  50. finally:
  51. connection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement