Question: Hi team Here I written the code in visual studio for webscrapping the data but the data is not saving in the excel sheet can

Hi team

Here I written the code in visual studio for webscrapping the data but the data is not saving in the excel sheet can u suggest me what is an error here

import scrapy

import openpyxl

class Spider1PySpider(scrapy.Spider):

name = 'Artisan_data'

allowed_domains = ['www.handicrafts.nic.in']

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!