首页 > scrapy爬取相对链接和绝对链接问题

scrapy爬取相对链接和绝对链接问题

示例代码如下:
示例中抓取的url是相对链接,在第7行中用urljoin转换为完整的链接。
那么,如果抓取的是绝对链接,不必转换,应该怎么写?

import scrapy
class StackOverflowSpider(scrapy.Spider):
name = 'stackoverflow'
start_urls = ['http://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,
    }

full_url = href

【热门文章】
【热门文章】