Neurolingua commited on
Commit
28bae9c
1 Parent(s): cc3fbd0

Update other_function.py

Browse files
Files changed (1) hide show
  1. other_function.py +26 -32
other_function.py CHANGED
@@ -114,43 +114,37 @@ def get_weather(city):
114
  import scrapy
115
  from scrapy.crawler import CrawlerProcess
116
  import pandas as pd
 
 
 
117
 
118
- class RatesSpider(scrapy.Spider):
119
- name = "rates_spider"
120
  start_urls = ['https://www.kisandeals.com/mandiprices/ALL/TAMIL-NADU/ALL']
121
 
122
  def parse(self, response):
123
- # Extract the table data
124
- table_rows = response.xpath('//table/tbody/tr')
125
-
126
- # Initialize a list to hold the data
127
- data = []
128
-
129
- for row in table_rows:
130
- # Extract the commodity name and price per kg
131
- commodity_name = row.xpath('td[1]//text()').get().strip()
132
- price_per_kg = row.xpath('td[2]//text()').get().strip()
133
-
134
- # Append the data to the list
135
- data.append((commodity_name, price_per_kg))
136
-
137
- # Convert the data to a Pandas DataFrame
138
- df = pd.DataFrame(data, columns=['Commodity', 'Price per kg'])
139
- # Convert the DataFrame to a dictionary
140
- rate_dict = df.set_index('Commodity')['Price per kg'].to_dict()
141
-
142
- # Return the scraped rates
143
- return rate_dict+' This is prices for 1 kg'
144
 
145
  def get_rates():
146
- process = CrawlerProcess({
147
- 'USER_AGENT': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0',
148
- 'LOG_LEVEL': 'ERROR', # Suppress logging to keep the output clean
149
- })
 
150
 
151
- spider = RatesSpider()
152
- process.crawl(spider)
153
- process.start() # This will block until the spider is done
 
 
 
 
154
 
155
- # Get the scraped data from the spider
156
- return spider.parse(None)
 
114
  import scrapy
115
  from scrapy.crawler import CrawlerProcess
116
  import pandas as pd
117
+ from scrapy.crawler import CrawlerProcess
118
+ from scrapy.utils.project import get_project_settings
119
+ import scrapy
120
 
121
+ class RateSpider(scrapy.Spider):
122
+ name = 'rates'
123
  start_urls = ['https://www.kisandeals.com/mandiprices/ALL/TAMIL-NADU/ALL']
124
 
125
  def parse(self, response):
126
+ rows = response.xpath('//table/tbody/tr')
127
+ data = {}
128
+ for row in rows:
129
+ commodity = row.xpath('td[1]/text()').get()
130
+ price = row.xpath('td[2]/text()').get()
131
+ data[commodity] = price
132
+ return data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  def get_rates():
135
+ # Set up a Scrapy process
136
+ process = CrawlerProcess(get_project_settings())
137
+
138
+ # Set up a dictionary to store the scraped data
139
+ data = {}
140
 
141
+ # Run the spider
142
+ def crawler_finished(signal, sender, item, response, spider):
143
+ data.update(item)
144
+
145
+ process.signals.connect(crawler_finished, signal=scrapy.signals.item_scraped)
146
+ process.crawl(RateSpider)
147
+ process.start() # This will block until the crawling is finished
148
 
149
+ # Return the scraped data as a string (or format as needed)
150
+ return str(data) + ' These prices are for 1 kg'