scrapy FormRequest True / Flase on /'off'复选框 [英] scrapy FormRequest True/Flase on/'off' Checked Boxes

查看:174
本文介绍了scrapy FormRequest True / Flase on /'off'复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类似的帖子中,有人询问有关将表单值从[ on]不打开,这只是设置一个True和False值(使用Mechanize)。

In a similar post a question was asked about changing a form value from [on] to not on, which was simply setting a 'True' and 'False' value (using Mechanize).

如何在scrapy FormRequest.from_response 中实现?

How would this be accomplished in scrapy FormRequest.from_response?

EDIT

例如,使用mechanize获取表单信息,

这是网页表单的默认值。

默认情况下,会检查表单上的所有内容:

EDIT
For example, using mechanize to get form information,
this is the default that comes with the webpage form.
By default, everything on the form is checked:

<CheckboxControl(ac=[*on])>
type=checkbox, name=ac value=['on']
<CheckboxControl(<None>=[*on])>
type=checkbox, name=None value=[]
<TextControl(p=)>
type=text, name=p value=
<CheckboxControl(pr[]=[*0, *1, *2])>
type=checkbox, name=pr[] value=['0', '1', '2']
<CheckboxControl(a[]=[*0, *1, *2, *3, *4])>
type=checkbox, name=a[] value=['0', '1', '2', '3', '4']
<CheckboxControl(pl=[*on])>
type=checkbox, name=pl value=['on']
<CheckboxControl(sp[]=[*1, *2, *3])>
type=checkbox, name=sp[] value=['1', '2', '3']
<SelectControl(pp=[0, 1, *2, 3])>
type=select, name=pp value=['2']

和' pl

它们的值为 [* on]

目标是关闭它们(?)(取消选中它们)

Note the 'ac', '<None>' and 'pl'.
They have a value of [*on]
The goal is to turn them 'off'(?) (uncheck them)

FormRequest.from_response(response, formnumber=0, formdata={'pr[]': '2', 'sp[]': '3', 'pp': '3', 'a[]': ['3', '4']}))

这会返回一个表单,其中包含每个formdata修改的框。
仍然检查formdata中未提及的那些键。

This returns a form with the modified boxes per the formdata. Those keys not mentioned in the formdata are still checked.

按照上述文章中的示例:

Following the example in the above post:

FormRequest.from_response(response, formdata={'live': 'False'})

我做了FormRequest有各种各样的值:'False','True','',[''],'on','off'和'None' t似乎得到正确的反应。

I have done the FormRequest with a variety of values: 'False', 'True', '', [''], 'on', 'off' and 'None' but can't seem to get the right response.

有任何建议吗?

EDIT:

尝试:


Have attempted:

FormRequest(url, formdata = {'pl': 'False'}, callback=parse_this)  
FormRequest(url, formdata = {'pl': 'off'}, callback=parse_this)  
FormRequest(url, formdata = {'pl': ''}, callback=parse_this) 
FormRequest(url, formdata = {'pl': 'None'}, callback=parse_this)
FormRequest(url, formdata = {'pl': None}, callback=parse_this) 

FormRequest.from_response(response, formdata = {'pl': 'False'})  
FormRequest.from_response(response, formdata = {'pl': 'off'})  
FormRequest.from_response(response, formdata = {'pl': '')  

默认情况下,网页提供了一个包含复选框的表单检查。目标是提交表单并关闭一个只有两个选项的复选框:'on'/'off'

By default, the webpage provides a form that contains checkboxes that are already checked. The goal is submit the form and 'turn off' some checkbox that only have two options: 'on'/'off'

推荐答案

复选框是一个类似任何其他输入字段,即它具有 value 属性,它被发送到服务器。唯一的区别是,如果没有选中,它不会被发送,如果它被选中,它将和其他字段一起发送。我的意思是一个服务器通常检查复选框是否被检查,只需检查它的名称是否在表单数据中。

Checkbox is an input field like any others, i.e. it has value attribute, which is sent to the server. The only difference is that if it is not checked, it is not sent at all, and if it is checked, it is sent along with other fields. I mean a server usually checks if a checkbox is checked by simply checking if its name is in the form data.

您要取消选中复选框生活。这意味着,它只需要不发送到服务器。

You want to "uncheck" checkbox called 'live'. That means that, it just has to be NOT sent to the server at all.

我会使用 FormRequest 的子类(未测试,但你应该得到这个想法):

I would use a subclass of FormRequest (not tested, but you should get the idea):

class MyFormRequest(FormRequest):
    """FormRequest subclass which filters from form data submitted to the server None values.
    This allows removing some fields automatically collected from a form by FormRequest.from_response method."""

    def __init__(self, *args, **kwargs):
        formdata = kwargs.get('formdata')
        if formdata: # filter out input fields with None values
            formdata = dict((name, value) for name, value in formdata.iteritems() if value is not None)
            kwargs['formdata'] = formdata

        super(MyFormRequest, self).__init__(*args, **kwargs)

然后使用 MyFormRequest.from_response ,而不是 FormRequest.from_response

另一个解决问题的方法是手动构造 FormRequest ,只传递需要的表单数据,而不使用 FormRequest.from_response

Another option to solve you problem is constructing FormRequest manually, passing it only that form data which is needed, without using FormRequest.from_response.

此处是未选中的复选框会发生什么:

Here is an example what happens with checkboxes which are unchecked:


在PHP脚本(checkbox-form.php)中,我们可以从$ _POST数组中获取提交的选项
。如果$ _POST ['formWheelchair']是是,则选中
框。如果未选中此复选框,则不会设置
$ _POST ['formWheelchair']。

In the PHP script (checkbox-form.php), we can get the submitted option from the $_POST array. If $_POST['formWheelchair'] is "Yes", then the box was checked. If the check box was not checked, $_POST['formWheelchair'] won't be set.

这篇关于scrapy FormRequest True / Flase on /'off'复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆