Web Scraping JavaScript-Enabled Websites with Scrapy-Selenium
Scrapy-selenium is a middleware used for web scraping<\/a>. Scrapy does not support scraping modern websites that use JavaScript frameworks, which is why this middleware is used together with Scrapy to scrape these modern sites<\/a>. Scrapy-selenium provides the capabilities of Selenium that help work with JavaScript-heavy sites. Another advantage is the driver, through which we can see what is happening behind the scenes. Since Selenium is an automated tool<\/a>, it also gives us the ability to work with input tags<\/a> and perform scraping based on what you enter in the input field. Scrapy-selenium was first introduced in 2018 and is open source. An alternative to it is scrapy-splash.<\/p>\n\n Installing and configuring Scrapy - <\/p>\n\n scrapy startproject projectname (projectname - the project name)<\/p>\n<\/blockquote>\n\n Now run,<\/p>\n\n scrapy genspider spidername example.com<\/p>\n<\/blockquote>\n\n (Replace spidername with your preferred spider name, and example.com with the site you want to scrape). Note: The URL can also be changed later inside your Scrapy <\/p>\n\n Install scrapy-selenium and add the following to the settings.py file <\/p>\n\n # for firefox # for chrome driver This project uses the Chrome driver. The Chrome driver must be downloaded according to your Chrome browser version. Go to the Chrome browser help section, then click About Google Chrome and check your version. command: scrapy crawl spidername (in this project, scrapy crawl integratedspider)<\/p>\n<\/blockquote>\n\n Spider code before scrapy-selenium: <\/p>\n\n import scrapy Important fields in scrapy-selenium:<\/p>\n\n General structure of a scrapy-selenium spider: <\/p>\n\n import scrapy Code for scraping course data from Geeksforgeeks - <\/p>\n\n import scrapy\n\t
\n
\n
<\/p>\n\n\n
\nfrom shutil import which
\n
\nSELENIUM_DRIVER_NAME = 'firefox'
\nSELENIUM_DRIVER_EXECUTABLE_PATH = which('geckodriver')
\nSELENIUM_DRIVER_ARGUMENTS=['-headless']
\n <\/p>\n<\/blockquote>\n\n\n
\nfrom shutil import which
\n
\nSELENIUM_DRIVER_NAME = 'chrome'
\nSELENIUM_DRIVER_EXECUTABLE_PATH = which('chromedriver')
\nSELENIUM_DRIVER_ARGUMENTS=['--headless']
\n DOWNLOADER_MIDDLEWARES = {
\n 'scrapy_selenium.SeleniumMiddleware': 800
\n }<\/p>\n<\/blockquote>\n\n
\n
\nRun the project:<\/p>\n\n\n
\n
\n
\nclass IntegratedspiderSpider(scrapy.Spider):
\n name = 'integratedspider' # spider name
\n allowed_domains = ['example.com']
\n start_urls = ['http:\/\/example.com\/']
\n
\n def parse(self, response):
\n pass<\/p>\n<\/blockquote>\n\n\n\t
<\/p>\n\n\n
\nfrom scrapy_selenium import SeleniumRequest
\n
\nclass IntegratedspiderSpider(scrapy.Spider):
\n name = 'integratedspider'
\n def start_requests(self):
\n yield SeleniumRequest(
\n url = "https:\/\/www.geeksforgeeks.org\/",
\n wait_time = 3,
\n screenshot = True,
\n callback = self.parse,
\n dont_filter = True
\n )
\n
\n def parse(self, response):
\n pass<\/p>\n<\/blockquote>\n\n\n
\nfrom scrapy_selenium import SeleniumRequest
\n
\nclass IntegratedspiderSpider(scrapy.Spider):
\n name = 'integratedspider'
\n def start_requests(self):
\n yield SeleniumRequest(
\n url = "https:\/\/practice.geeksforgeeks.org\/courses\/online",
\n wait_time = 3,
\n screenshot = True,
\n callback = self.parse,
\n dont_filter = True
\n )
\n
\n def parse(self, response):
\n # courses make list of all items that came in this xpath
\n # this xpath is of cards containing courses details
\n courses = response.xpath('\/*[@id ="active-courses-content"]\/div\/div\/div')
\n
\n # course is each course in the courses list
\n for course in courses:
\n # xpath of course name is added in the course path
\n # text() will scrape text from h4 tag that contains course name
\n course_name = course.xpath('.\/a\/div[2]\/div\/div[2]\/h4\/text()').get()
\n
\n # course_name is a string containing \\n and extra spaces
\n # these \\n and extra spaces are removed
\n
\n course_name = course_name.split('\\n')[1]
\n course_name = course_name.strip()
\n
\n
\n yield {
\n 'course Name':course_name
\n }<\/p>\n<\/blockquote>


