Spaces:
Sleeping
Sleeping
Update other_function.py
Browse files- 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
|
| 119 |
-
name =
|
| 120 |
start_urls = ['https://www.kisandeals.com/mandiprices/ALL/TAMIL-NADU/ALL']
|
| 121 |
|
| 122 |
def parse(self, response):
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 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 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
return
|
|
|
|
| 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'
|