• scray cookiejar


    参考: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)
  • 相关阅读:
    vue 中的registerServiceWorker
    转载:阿里手淘可伸缩布局方案amfe-flexible解决vue移动端适配问题
    转载:vue-clipboard2(vue剪切板功能)
    vue中节流函数实现搜索数据
    vue项目怎么阻止很快速的点击两次然后提交的两次请求
    IE10中show-overflow-tooltip不展示
    Oracle锁表查询和解锁方法
    自动重启sqlserver服务
    利用jQuery中live为动态生成Dom添加datepicker效果
    IIS7 https 发生413错误 未显示页面,因为请求实体过大
  • 原文地址:https://www.cnblogs.com/testzcy/p/14772619.html
Copyright © 2020-2023  润新知