Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. def parse(self, response):
  2. with open('quotes-data.csv', 'w') as output_file:
  3. csv_writer = csv.writer(output_file, delimiter='t', quotechar="'")
  4.  
  5. csv_writer.writerow(['title', 'author', 'description', 'tags'])
  6. i = 1
  7. for quote in response.xpath('//div[@class="book"]'):
  8. title = quote.xpath('./div[@class="title"]/text()').extract_first()
  9. author = quote.xpath('.//div[@class="author"]/text()').extract_first()
  10. description = quote.xpath('.//div[@class="description"]/text()').extract_first()
  11. tags = quote.xpath('.//div[@class="keywords"]/span[@class="tag"]/text()').extract()
  12. tags = ' '.join(tags)
  13. tags = f'"{tags}"'
  14. author = f'"{author}"'
  15. description = f'"{description}"'
  16. row = [i, author, title, description, tags]
  17. csv_writer.writerow(row)
  18.  
  19. i += 1
  20.  
  21. yield {
  22. 'title': title,
  23. 'author': author,
  24. 'tags': tags,
  25. 'description': description
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement