1

I just started crawling. I'm trying to crawl question and answers from website http://www.indiabix.com/verbal-ability/spotting-errors/ by downloading content using Scrapy framework and Python 2.7. I noticed that if you view its source, you'll notice that answer for every question should be in the b tag but its not:

<div class="div-spacer">
   <p><span class="ib-green"><b>Answer:</b></span> Option <b class="jq-hdnakqb"></b></p> 
   <p><span class="ib-green"><b>Explanation:</b></span></p> 
   <p> No answer description available for this question. <b><a href="discussion-399">Let us discuss</a></b>. </p>

If we inspect element on the webpage we can see the correct answer as text between the tags : Answer: Option A or B etc. for each question but the HTML source code doesn't.

To get the text within the b tag I've tried around 15 queries using xpath. Ive written the most probable 4-5 queries as comments in the code below.

import scrapy
import urllib
import json
from errors1.items import Errors1Item

class Errors1Spider(scrapy.Spider) :
    name = "errors1"
    start_urls =  ["http://www.indiabix.com/verbal-ability/spotting-errors/"]

    def parse(self, response) :
        i = 0
        y = 0
        j = json.loads(json.dumps(response.xpath('//td[contains(@id, "tdOption")]/text()').extract()))
        x = json.loads(json.dumps(response.xpath('//div[@class="div-spacer"]/p[3]/text()').extract()))

        #to get correct answer
        #response.xpath('//div[@class = "div-spacer"]/p/b/text()').extract()
        #response.xpath('//div[@class = "div-spacer"]/p[1]/b/text()').extract()
        #response.xpath('//div[@class = "div-spacer"]/p//text()').extract()
        #response.xpath('//b[@class = "jq-hdnakqb"]/text()').extract()
        #response.xpath('string(//div[@class = "div-spacer"]/p/b/text())').extract()

        while i<len(j) and y<len(x) :
            item = Errors1Item()
            item['optionA'] = j[i]
            i+=1 
            item['optionB'] = j[i]
            i+=1 
            item['optionC'] = j[i]
            i+=1 
            item['optionD'] = j[i]
            i+=1 
            item['explanation'] = x[y]
            y+=1
            yield item

Can someone please help me get the answer content from that webpage. Thanks

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
pratibha
  • 21
  • 1
  • 2

1 Answers1

1

From what I understand, there is a javascript logic involved in setting the correct option value.

What helped me to solve it is scrapyjs middleware, that uses Splash browser-as-a-service. Skipping the installation and configuration, here is the spider that I've executed:

# -*- coding: utf-8 -*-
import scrapy


class IndiaBixSpider(scrapy.Spider):
    name = "indiabix"
    allowed_domain = ["www.indiabix.com"]
    start_urls = ["http://www.indiabix.com/verbal-ability/spotting-errors/"]

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url, meta={
                'splash': {
                    'endpoint': 'render.html',
                    'args': {'wait': 0.5}
                }
            })

    def parse(self, response):
        for question in response.css("div.bix-div-container"):
            answer = question.xpath(".//input[starts-with(@id, 'hdnAnswer')]/@value").extract()
            print answer

And here is what I've got on the console (correct answers):

[u'A']
[u'C']
[u'A']
[u'C']
[u'C']
[u'C']
[u'B']
[u'A']
[u'D']
[u'C']
[u'B']
[u'B']
[u'A']
[u'B']
[u'B']

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I tried to run this exact same code but i still dont get any output and im getting an empty set. I think i might have configured it wrong. can you send me your settings.py file? – pratibha Jul 07 '15 at 05:50
  • @pratibha they are exactly the same as in the linked answer. Also, make sure the splash is running. – alecxe Jul 08 '15 at 01:25