noam76

scrapy1

May 9th, 2023 (edited)
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import scrapy
  2. import pandas as pd
  3.  
  4. class MySpider(scrapy.Spider):
  5.     name = 'myspider'
  6.     start_urls = ['https://solarkeymark.eu/database/']
  7.    
  8.     def parse(self, response):
  9.         for link in response.css('a[href^="/database/manufacturer/"]::attr(href)').getall():
  10.             yield response.follow(link, callback=self.parse_manufacturer)
  11.  
  12.     def parse_manufacturer(self, response):
  13.         name = response.css('h1::text').get().strip()
  14.         for product in response.css('div.row.table-wrapper table tbody tr'):
  15.             brand = product.css('td:nth-child(1)::text').get()
  16.             license_number = product.css('td:nth-child(2)::text').get()
  17.             test_results_link = product.css('td:nth-child(3) a::attr(href)').get()
  18.             if 'thermal' in test_results_link:
  19.                 yield response.follow(test_results_link,
  20.                     callback=self.parse_test_results,
  21.                     meta={
  22.                         'name': name,
  23.                         'brand': brand,
  24.                         'license_number': license_number
  25.                     })
  26.  
  27.     def parse_test_results(self, response):
  28.         name = response.meta['name']
  29.         brand = response.meta['brand']
  30.         license_number = response.meta['license_number']
  31.         values = response.css('.list-group-item span.float-right::text').getall()
  32.         eta_0_b = values[0]
  33.         a1 = values[1]
  34.         a2 = values[2]
  35.         a3 = values[3]
  36.         data = {
  37.             'Name': [name],
  38.             'Brand': [brand],
  39.             'License Number': [license_number],
  40.             'Eta_0_b': [eta_0_b],
  41.             'a1': [a1],
  42.             'a2': [a2],
  43.             'a3': [a3]
  44.         }
  45.         df = pd.DataFrame(data)
  46.         df.to_excel('test_results.xlsx', index=False)
  47.  
Advertisement
Add Comment
Please, Sign In to add comment