Question
Here Below I have written the code for extracting the data from the given url but the data is not saving in the excel sheet
Here Below I have written the code for extracting the data from the given url but the data is not saving in the excel sheet so please let me know why the data is not saving in the excel sheet below is the code can u check it please
import scrapy
import openpyxl
from openpyxl import workbook
import xlsxwriter
class ArtisanDataSpider(scrapy.Spider):
name = "artisan_data"
start_urls = [
'http://www.handicrafts.nic.in/ArtisanData.aspx?MID=SZmOd%2fCrxTo9CHD2XKF+pA%3d%3d'
]
def parse(self, response):
# Set state to Uttar Pradesh
state_selector = response.css("#ddlState option[value='Uttar Pradesh']")
state_selector.attrib['selected'] = 'selected'
# Set districts to SANT RAVIDAS NAGAR, AGRA, VARANASI
districts = ['SANT RAVIDAS NAGAR', 'AGRA', 'VARANASI']
district_selectors = response.css("#ddlDistrict option")
for district_selector in district_selectors:
if district_selector.css("::text").get() in districts:
district_selector.attrib['selected'] = 'selected'
# Click the submit button
submit_button = response.css("#btnSearch")
request = scrapy.FormRequest.from_response(
response,
formid='form1',
formdata={'__EVENTTARGET': submit_button.attrib['name'], '__EVENTARGUMENT': ''},
callback=self.parse_data
)
yield request
def parse_data(self, response):
# Extract data from the table
rows = response.css("table#gvArtisan tr")
data = []
for row in rows:
cells = row.css("td")
if cells:
data.append([cell.css("::text").get() for cell in cells])
# Save data to an Excel sheet
wb = openpyxl.Workbook()
sheet = wb.active
for row in data:
sheet.append(row)
wb.save("artisan_data.xlsx")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started