Python 用scrapy选择单选按钮

Python 用scrapy选择单选按钮,python,scrapy,Python,Scrapy,我如何使用scrapy选择单选按钮 我正在尝试选择以下选项 formdata={'rd1':'E'} does not work <input type="radio" name="rd1" value="E" checked="checked" />Employee <input type="radio" name="rd2" value="o" />Other formdata={'rd1':'E'}不起作用 雇员 其他 您可以使用来选择单选按钮 >>

我如何使用scrapy选择单选按钮

我正在尝试选择以下选项

formdata={'rd1':'E'} does not work

<input type="radio" name="rd1" value="E" checked="checked" />Employee
<input type="radio" name="rd2" value="o" />Other
formdata={'rd1':'E'}不起作用
雇员
其他
您可以使用来选择单选按钮

>>> import lxml.html
>>> from lxml.cssselect import CSSSelector
>>> str = """
... '<input type="radio" name="rd1" value="E" checked="checked" />Employee
... <input type="radio" name="rd2" value="o" />Other'
... """
>>> input_sel = CSSSelector('input[name="rd1"]')
>>> lx = lxml.html.fromstring(str)
>>> input_sel(lx)
[<InputElement b7e7665c name='rd1' type='radio'>]
导入lxml.html >>>从lxml.cssselect导入CSSSelector >>>str=”“” ……雇员 …其他的 ... """ >>>input_sel=CSSSelector('input[name=“rd1”]”) >>>lx=lxml.html.fromstring(str) >>>输入选择(lx) []
我刚刚遇到了一个类似的问题(当然这就是我来这里的原因)。这个精彩的芝加哥城市网站()要求你的机器人通过单选按钮和点击按钮“Aggree”到他们的“责任免责声明”(这真的很有趣!)。我通过
scrapy.FormRequest.from\u response
的帮助解决了这个问题:

def agreement_failed(response):
    # check the result of your first post here
    return  # something if it's a failure or nothing if it's not


class InspectionsListSpider(scrapy.Spider):
    name = 'inspections_list'
    start_urls = ['https://webapps1.chicago.gov/buildingrecords/home']

    def parse(self, response):
        return scrapy.FormRequest.from_response(
            response,
            formid='agreement',
            formdata = {"agreement": "Y",
                        "submit": "submit"},
            callback = self.after_agreement
            )

    def after_agreement(self, response):
        if agreement_failed(response):
            self.logger.error("agreement failed!")
            return
        else:
            ... # whatever you are going to do after
再加上页面的代码,它是非常不言自明的。您可能还需要此处描述的表单的其他参数:

另外,下一页的谜语也是可以用同样的方法解决的