添加新功能后脚本不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加新功能后脚本不起作用相关的知识,希望对你有一定的参考价值。

我正在尝试使用python scrapy实现登录。但是,登录无法正常工作。我指的是下面的代码

from scrapy.http import Request, FormRequest
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
# from scrapy.selector import htmlXPathSelector
from scrapy.http import FormRequest
import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.utils.response import open_in_browser


class LoginSpider(CrawlSpider):
    name = 'loginspider'
    login_page = 'http://quotes.toscrape.com/login'
    start_urls = ['http://quotes.toscrape.com/login']
    username = 'foobar'
    password = 'foobar'

    def init_request(self):
        return Request(url=self.login_page, callback=self.start_requests)

    def start_requests(self):
        print("
 start_request is here 
")
        yield Request(
            url=self.login_page, callback=self.login, dont_filter=True)

    def login(self, response):
        print("
 Login is here! 
")
        token = response.xpath(
            '//*[@name="csrf_token"]/@value').extract_first()
        return FormRequest.from_response(
            response,
            formdata={
                'csrf_token': token,
                'password': 'foobar',
                'username': 'foobar'
            },
            callback=self.check_login_response)

    def check_login_response(self, response):
        open_in_browser(response)
        print("
 Check_login_response 
")
        if b"Learn" in response.body:
            print("Worked, logged in")
            #return self.parse_item
        else:
            print("Not logged in")
            return


process = CrawlerProcess()
process.crawl(LoginSpider)
process.start()

从上面的代码而不是对凭据进行硬编码,我如下更改代码

 def fetch_form_data(self,response):
        if all(field in response.text for field in self.credentials['fields_in_response']):
            inputs =response.xpath('//form//input').extract()
            soup_dict={}
            for key,i in enumerate(inputs):
                soup = BeautifulSoup(i, 'html.parser')
                inp_type   =   soup.input['type'] if soup.input.has_attr('type') else None
                inp_value  =   soup.input['value'] if soup.input.has_attr('value')  else None
                inp_name   =   soup.input['name'] if soup.input.has_attr('name')  else None
                soup_dict[key]= {'name':inp_name,'value':inp_value,'type':inp_type}
            login_cred= self.credentials['login_details']
            form_data={}
            for key,value in soup_dict.items():
                if value['name'] != None and value['type'] == 'text':
                    form_data[value['name']]=login_cred['name']
                elif value['name'] != None and value['type'] == 'password':
                    form_data[value['name']]=login_cred['pwd']
                elif value['name'] != None and value['type'] == 'hidden':
                    form_data[value['name']]=value['value']
                else:
                    pass
            return form_data      

    def login(self, response):
        print ("
 Login is here! 
")
        formdata=self.fetch_form_data(response)         
        return FormRequest.from_response(response,
        formdata,
        callback=self.check_login_response)

但是现在登录不起作用。我想我在其中缺少一些东西。有人可以向我建议这是什么

运行脚本时我注意到的区别是enter image description here

实际上运行第一个脚本时,中间件会发生重定向。enter image description here在这种情况下,它不会发生

答案

函数login()在函数fetch_form_data()中...

以上是关于添加新功能后脚本不起作用的主要内容,如果未能解决你的问题,请参考以下文章

光滑的轮播“初始化”功能不起作用

添加到片段时按钮不起作用

如何将按钮功能添加到片段中

替换或删除后台堆栈上现有片段的代码不起作用

添加jQuery脚本后提交按钮不起作用

FragmentNavigator 共享过渡不起作用