选择带有碎屑的单选按钮

2024-05-13 05:10:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我该如何使用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

Tags: nameinputvaluetype选项employeenot按钮
2条回答

您可以使用lxml.cssselector选择单选按钮。在

>>> 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'>]

我刚刚遇到了一个类似的问题(这就是我来这里的原因)。芝加哥城的这个奇妙的网站(https://webapps1.chicago.gov/buildingrecords/home)要求你的机器人“Aggree”他们的“责任免责声明”(这确实很有趣!)用单选按钮和单击按钮。我在scrapy.FormRequest.from_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

加上页面的代码,这是不言而喻的。您可能还需要此处描述的表单的其他参数:https://docs.scrapy.org/en/latest/topics/request-response.html?highlight=FormRequest()#scrapy.http.FormRequest.from_response

下一页的谜语也是可以用同样的方法来解决的。:)

相关问题 更多 >