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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!