我已经使用pip安装了scrapy,并尝试了scrapy文档中的示例。

我收到错误cannot import name xmlrpc_client
在研究了stachoverflow问题here之后
我已经使用修复它

sudo pip uninstall scrapy

sudo pip install scrapy==0.24.2

但现在它显示了exceptions.AttributeError: 'HtmlResponse' object has no attribute 'urljoin'
这是我的代码:
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,
        }

谁能帮我这个忙!

最佳答案

在Scrapy> = 0.24.2中,HtmlResponse类尚无urljoin()方法。直接使用 urlparse.urljoin() :

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

不要忘记导入它:
import urlparse

请注意,在Scrapy 1.0中添加了urljoin()别名/helper,这是相关的问题:
  • Add Response.urljoin() helper

  • 这是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)
    

    关于python - 严重错误: exceptions. AttributeError: 'HtmlResponse'对象没有属性 'urljoin',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31145690/

    10-13 09:31