Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

Th e complaint department was closed over the lunch hour.

Answered: 1 week ago

Question

Understand how customers respond to effective service recovery.

Answered: 1 week ago