Scrapy错误:exceptions.AttributeError:'HtmlResponse'对象没有属性'urljoin'

use*_*315 5 python scrapy web-scraping

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

我收到了错误 cannot import name xmlrpc_client

在查看stachoverflow问题后, 我已经使用它修复了它

sudo pip uninstall scrapy

sudo pip install scrapy==0.24.2
Run Code Online (Sandbox Code Playgroud)

但现在它告诉我 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,
        }
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我!

ale*_*cxe 6

在Scrapy> = 0.24.2中,HtmlResponse类还没有urljoin()方法.urlparse.urljoin()直接使用:

full_url = urlparse.urljoin(response.url, href.extract())
Run Code Online (Sandbox Code Playgroud)

别忘了导入它:

import urlparse
Run Code Online (Sandbox Code Playgroud)

请注意,urljoin()在Scrapy 1.0中添加了别名/帮助程序,这是相关问题:

这里是它实际上是什么:

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)
Run Code Online (Sandbox Code Playgroud)