5

I have installed scrapy using pip and tried the example in the scrapy documentation.

I got the error cannot import name xmlrpc_client

After looking into a stachoverflow question here i have fixed it using

sudo pip uninstall scrapy

sudo pip install scrapy==0.24.2

but now it shows me exceptions.AttributeError: 'HtmlResponse' object has no attribute 'urljoin'

Here is my code :

import scrapy


class StackOverflowSpider(scrapy.Spider):
    name = 'stackoverflow'
    start_urls = ['https://stackoverflow.com/questions?sort=votes']

    def parse(self, response):
        for href in response.css('.question-summary h3 a::attr(href)'):
            full_url = response.urljoin(href.extract())
            yield scrapy.Request(full_url, callback=self.parse_question)

    def parse_question(self, response):
        yield {
            'title': response.css('h1 a::text').extract()[0],
            'votes': response.css('.question .vote-count-post::text').extract()[0],
            'body': response.css('.question .post-text').extract()[0],
            'tags': response.css('.question .post-tag::text').extract(),
            'link': response.url,
        }

Can anyone please help me with this!

Community
  • 1
  • 1
user3437315
  • 270
  • 4
  • 13

2 Answers2

6

In Scrapy >=0.24.2, HtmlResponse class does not yet have urljoin() method. Use urlparse.urljoin() directly:

full_url = urlparse.urljoin(response.url, href.extract())

Don't forget to import it:

import urlparse

Note that urljoin() alias/helper was added in Scrapy 1.0, here is the related issue:

And here is what it actually is:

from six.moves.urllib.parse import urljoin

def urljoin(self, url):
    """Join this Response's url with a possible relative url to form an
    absolute interpretation of the latter."""
    return urljoin(self.url, url)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

The example code you're using is for scrapy 1.0. Since you've downgraded to 0.24, you need to use the urljoin from urlparse:

full_url = urljoin(response.url, href.extract())

If you click the "Scrapy 0.24 (old stable)" button above the example you'll get example code for the scrapy version you're using.

Erin Call
  • 1,764
  • 11
  • 15