Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Show the work so I can understand how to do it please. Thank you

Answered: 1 week ago

Question

LO1 Identify why performance management is necessary.

Answered: 1 week ago