首页 > 编程语言 > 详细

scray cookiejar

时间:2021-05-16 00:20:03      阅读:16      评论:0      收藏:0      [点我收藏+]

参考:https://blog.csdn.net/u013210620/article/details/80283637

手动操作cookie点赞

# -*- coding: utf-8 -*-
import scrapy
from bs4 import BeautifulSoup
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from ..items import XianglongItem
from scrapy.http import HtmlResponse
from scrapy.http.response.html import HtmlResponse


class ChoutiSpider(scrapy.Spider):
    name = chouti
    allowed_domains = [chouti.com]
    start_urls = [http://dig.chouti.com/,]

    cookie_dict = {}
    def start_requests(self):
        for url in self.start_urls:
            yield Request(url=url,callback=self.parse_index)

    def parse_index(self,response):
        # 原始cookie
        # print(response.headers.getlist(Set-Cookie))

        # 解析后的cookie
        from scrapy.http.cookies import CookieJar
        cookie_jar = CookieJar()
        cookie_jar.extract_cookies(response, response.request)
        print("cookie_jar._cookies",cookie_jar._cookies)
        for k, v in cookie_jar._cookies.items():
            for i, j in v.items():
                for m, n in j.items():
                    self.cookie_dict[m] = n.value


        req = Request(
            url=http://dig.chouti.com/login,
            method=POST,
            headers={Content-Type: application/x-www-form-urlencoded; charset=UTF-8},
            body=phone=xxx&password=xxx&oneMonth=1,
            cookies=self.cookie_dict,
            callback=self.parse_check_login
        )
        yield req

    def parse_check_login(self,response):
        print("parse_check_login",response.text)
        yield Request(
            url=https://dig.chouti.com/link/vote?linksId=19440976,
            method=POST,
            cookies=self.cookie_dict,
            callback=self.parse_show_result
        )

    def parse_show_result(self,response):
        print("parse_show_result",response.text)

自动操作cookie点赞

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
class ChoutiSpider(scrapy.Spider):
    name = chouti
    allowed_domains = [chouti.com]
    start_urls = [http://dig.chouti.com/,]

    def start_requests(self):
        for url in self.start_urls:
            yield Request(url=url,callback=self.parse_index,meta={cookiejar:True})

    def parse_index(self,response):
        req = Request(
            url=http://dig.chouti.com/login,
            method=POST,
            headers={Content-Type: application/x-www-form-urlencoded; charset=UTF-8},
            body=phone=8613121758648&password=woshiniba&oneMonth=1,
            callback=self.parse_check_login,
            meta={cookiejar: True}
        )
        yield req

    def parse_check_login(self,response):
        # print(response.text)
        yield Request(
            url=https://dig.chouti.com/link/vote?linksId=19440976,
            method=POST,
            callback=self.parse_show_result,
            meta={cookiejar: True}
        )

    def parse_show_result(self,response):
        print(response.text)

 

scray cookiejar

原文:https://www.cnblogs.com/testzcy/p/14772619.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!