Scrapy cookie接受表单

时间:2017-04-09 12:13:56

标签: cookies scrapy

我考虑并尝试了各种选择,即: - FormRequests - 传递cookie

可悲的是,我一直陷入困境:https://www.marktplaats.nl/cookiewall/?target=https%3A%2F%2Fwww.marktplaats.nl%2F

class MarktplaatsSpider(CrawlSpider):
    name = 'MarktplaatsSpidertest'
    source = 'Markplaats.nl'
    allowed_domains = ['marktplaats.nl']
    start_urls = ['https://www.marktplaats.nl/']

    rules = [Rule(LinkExtractor(allow=()), callback='parse_item',follow=True)]

    def start_request(self):
        form_data = {'CookieOptIn':'true'}
        request_body = json.dumps(form_data)
        yield scrapy.Request('https://www.marktplaats.nl',
                            method="POST",
                            body=request_body,
                            headers={'Content-Type': 'application/json; charset=UTF-8'}, )

   def parse_item(self, response):
       print(response.url)
       item['URL'] = response.url

       yield item(source=self.source, URL=item['URL'], hash = get_hash(response.url))

还有其他一些网站我遇到了同样的问题。我根本不知道我的蜘蛛如何到达页面。

任何人都可以帮助我/指出正确的方向吗?

此致

Durk

1 个答案:

答案 0 :(得分:1)

试试这个。

cookies = {
    'CookieOptIn': 'true',
    'luckynumber': '1896761001',
    'MpSession': '9ff31f05-36fd-4570-9cdc-e1800bf682fe',
}

headers = {
    'Pragma': 'no-cache',
    'DNT': '1',
    'Accept-Encoding': 'gzip, deflate, sdch, br',
    'Accept-Language': 'en-US,en;q=0.8',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Cache-Control': 'no-cache',
    'Referer': 'https://www.marktplaats.nl/cookiewall/?target=https%3A%2F%2Fwww.marktplaats.nl%2F',
    'Connection': 'keep-alive',
}

yield Request('https://www.marktplaats.nl/', headers=headers, cookies=cookies)