scrapy爬取豆瓣电影top250,请求内容正常,但请求robots.txt返回403

MrChi
  • 489

使用scrapy写了一个小demo,爬取豆瓣电影的top250排行。加了User-Agent反爬虫,内容已经可以正常爬取了,但robots文件(https://movie.douban.com/robo...)一直403。

尝试增加AcceptAccept-EncodingAccept-LanguageReferer伪装都没有效果。目前找不到原因是怎么造成的。

个人猜测:

  1. 网站设置问题。但是对于robots.txt文件的反爬虫比内容还有严格,是不是有点本末倒置?
  2. Scrapy机制问题。是否是Scrapy请求robots.txt时没有使用我设置的User-Agent,由于刚接触,各方面了解的不多,还请大家见谅。

附上spider代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from scrapy import Request
from scrapy.spiders import Spider
from douban.items import DoubanMovieItem

class DoubanMovieTop250Spider(Spider):
    name = 'douban_movie_top250'

    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4',
        'Referer': 'https://movie.douban.com/top250'
    }

    def start_requests(self):
        url = 'https://movie.douban.com/top250'
        yield Request(url, headers=self.headers)

    def parse(self, response):
        item = DoubanMovieItem()
        movies = response.css('ol.grid_view>li')
        for movie in movies:
            item['ranking'] = movie.css('div.pic>em::text').extract()[0]
            yield item

日志输出(debug级别):

2017-09-03 11:02:46 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: douban)
2017-09-03 11:02:46 [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': 'douban', 'NEWSPIDER_MODULE': 'douban.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['douban.spiders']}
2017-09-03 11:02:46 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.logstats.LogStats']
2017-09-03 11:02:46 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2017-09-03 11:02:46 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2017-09-03 11:02:46 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-09-03 11:02:46 [scrapy.core.engine] INFO: Spider opened
2017-09-03 11:02:46 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-09-03 11:02:46 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-09-03 11:02:46 [scrapy.core.engine] DEBUG: Crawled (403) <GET https://movie.douban.com/robots.txt> (referer: None)
2017-09-03 11:02:46 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://movie.douban.com/top250> (referer: https://movie.douban.com/top250)
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '1'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '2'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '3'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '4'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '5'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '6'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '7'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '8'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '9'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '10'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '11'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '12'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '13'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '14'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '15'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '16'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '17'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '18'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '19'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '20'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '21'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '22'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '23'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '24'}
2017-09-03 11:02:46 [scrapy.core.scraper] DEBUG: Scraped from <200 https://movie.douban.com/top250>
{'ranking': '25'}
2017-09-03 11:02:46 [scrapy.core.engine] INFO: Closing spider (finished)
2017-09-03 11:02:46 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 634,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 13139,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 1,
 'downloader/response_status_count/403': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2017, 9, 3, 3, 2, 46, 749904),
 'item_scraped_count': 25,
 'log_count/DEBUG': 28,
 'log_count/INFO': 7,
 'memusage/max': 45244416,
 'memusage/startup': 45244416,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2017, 9, 3, 3, 2, 46, 175707)}
2017-09-03 11:02:46 [scrapy.core.engine] INFO: Spider closed (finished)
回复
阅读 5.3k
4 个回答
✓ 已被采纳

问题原因找到了,是因为在Spider中设置的header仅仅用于请求内容。而请求robots.txt时,Scrapy用的是默认UA配置,于是就被拦截了T_T

正确设置UA的方式,应该是在config中进行设置。

我看过一片关于豆瓣爬虫的知乎文章,里面有这段评论:
图片描述
你看看是不是因为这个原因

settings

Obey robots.txt rules
ROBOTSTXT_OBEY = False

如果你是固定爬虫路线了,就没必要爬robots.tx了。

没有ua肯定不让访问啊 ua是最基本的过滤手段了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏