如何使用scrapy CrawlSpider请求发送cookie?

时间:2015-09-17 05:43:39

标签: python cookies web-scraping scrapy

我正在尝试使用Python的Reddit框架创建此Scrapy剪贴簿。

我使用CrawSpider来抓取Reddit及其subreddits。但是,当我遇到包含成人内容的网页时,网站会要求提供Cookie over18=1

所以,我一直在尝试发送一个cookie,其中包含蜘蛛所做的每一个请求,但是,它没有用完。

这是我的蜘蛛代码。如您所见,我尝试使用start_requests()方法为每个蜘蛛请求添加一个cookie。

这里有人能告诉我怎么做吗?或者我做错了什么?

from scrapy import Spider
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from reddit.items import RedditItem
from scrapy.http import Request, FormRequest

class MySpider(CrawlSpider):
    name = 'redditscraper'
    allowed_domains = ['reddit.com', 'imgur.com']
    start_urls = ['https://www.reddit.com/r/nsfw']

    rules = (
        Rule(LinkExtractor(
            allow=['/r/nsfw/\?count=\d*&after=\w*']),
            callback='parse_item',
            follow=True),
    )

    def start_requests(self):
        for i,url in enumerate(self.start_urls):
            print(url)
            yield Request(url,cookies={'over18':'1'},callback=self.parse_item)

    def parse_item(self, response):
        titleList = response.css('a.title')

        for title in titleList:
            item = RedditItem()
            item['url'] = title.xpath('@href').extract()
            item['title'] = title.xpath('text()').extract()
            yield item

4 个答案:

答案 0 :(得分:14)

好。尝试做这样的事情。

def start_requests(self):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36'}
    for i,url in enumerate(self.start_urls):
        yield Request(url,cookies={'over18':'1'}, callback=self.parse_item, headers=headers)

它是阻止你的用户代理。

编辑:

不知道CrawlSpider有什么问题,但Spider无论如何都可以。

#!/usr/bin/env python
# encoding: utf-8
import scrapy


class MySpider(scrapy.Spider):
    name = 'redditscraper'
    allowed_domains = ['reddit.com', 'imgur.com']
    start_urls = ['https://www.reddit.com/r/nsfw']

    def request(self, url, callback):
        """
         wrapper for scrapy.request
        """
        request = scrapy.Request(url=url, callback=callback)
        request.cookies['over18'] = 1
        request.headers['User-Agent'] = (
            'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, '
            'like Gecko) Chrome/45.0.2454.85 Safari/537.36')
        return request

    def start_requests(self):
        for i, url in enumerate(self.start_urls):
            yield self.request(url, self.parse_item)

    def parse_item(self, response):
        titleList = response.css('a.title')

        for title in titleList:
            item = {}
            item['url'] = title.xpath('@href').extract()
            item['title'] = title.xpath('text()').extract()
            yield item
        url = response.xpath('//a[@rel="nofollow next"]/@href').extract_first()
        if url:
            yield self.request(url, self.parse_item)
        # you may consider scrapy.pipelines.images.ImagesPipeline :D

答案 1 :(得分:4)

The Scrapy Docs

1.使用词典:

request_with_cookies = Request(url="http://www.example.com",
                               cookies={'currency': 'USD', 'country': 'UY'})

2.使用dicts列表:

request_with_cookies = Request(url="http://www.example.com",
                               cookies=[{'name': 'currency',
                                        'value': 'USD',
                                        'domain': 'example.com',
                                        'path': '/currency'}])

答案 2 :(得分:3)

您也可以通过标题发送。

scrapy.Request(url=url, callback=callback, headers={'Cookie':my_cookie})

答案 3 :(得分:1)

您可以在规则中使用process_request参数,例如:

    rules = (
    Rule(LinkExtractor(
        allow=['/r/nsfw/\?count=\d*&after=\w*']),
        callback='parse_item',
        process_request='ammend_req_header',
        follow=True)

    def ammend_req_header(self, request):
        request.cookies['over18']=1
        return request