Advertisement
noam76

solkey_spider.py

May 9th, 2023 (edited)
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import scrapy
  2. from ..items import SolkeyItem
  3. from openpyxl import Workbook
  4.  
  5. class SolkeySpider(scrapy.Spider):
  6.     name = 'solkey'
  7.     allowed_domains = ['www.duurzaamloket.nl']
  8.     start_urls = ['https://www.duurzaamloket.nl/SolKey_X014/index.php?SchemeNo=0&Offset=1&SearchText=&PageCnt=448']
  9.  
  10.     def parse(self, response):
  11.         wb = Workbook()
  12.         ws = wb.active
  13.         ws.append(['ID', 'Nom', 'Eta', 'b', 'a1', 'a2', 'a3'])
  14.        
  15.         rows = response.xpath('//table[@id="mytable"]/tr')[1:] # Récupération de toutes les lignes de la table
  16.        
  17.         for row in rows:
  18.             item = SolkeyItem()
  19.            
  20.             # Récupération de l'ID et du nom de la licence
  21.             item['id'] = row.xpath('.//td[1]/text()').get()
  22.             item['name'] = row.xpath('.//td[2]/text()').get()
  23.            
  24.             # Récupération de la page de détails et extraction des données η, b, a1, a2, a3
  25.             detail_page = row.xpath('.//td[2]/a/@href').get()
  26.             yield response.follow(detail_page, self.parse_detail, meta={'item': item, 'ws': ws})
  27.            
  28.         wb.save("solkey_results.xlsx") # Sauvegarde des données dans un fichier Excel
  29.    
  30.     def parse_detail(self, response):
  31.         item = response.meta['item']
  32.         ws = response.meta['ws']
  33.        
  34.         item['eta'] = response.xpath('//th[text()="η0"]/following-sibling::td[1]/text()').get()
  35.         item['b'] = response.xpath('//th[text()="b"]/following-sibling::td[1]/text()').get()
  36.         item['a1'] = response.xpath('//th[text()="a1"]/following-sibling::td[1]/text()').get()
  37.         item['a2'] = response.xpath('//th[text()="a2"]/following-sibling::td[1]/text()').get()
  38.         item['a3'] = response.xpath('//th[text()="a3"]/following-sibling::td[1]/text()').get()
  39.        
  40.         ws.append([item['id'], item['name'], item['eta'], item['b'], item['a1'], item['a2'], item['a3']])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement