我如何选择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
答案 0 :(得分:1)
您可以使用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'>]
答案 1 :(得分:0)
我刚刚遇到一个类似的问题(这就是为什么我在这里的原因)。这个位于芝加哥市(https://webapps1.chicago.gov/buildingrecords/home)的奇妙站点要求您的机器人通过单选按钮并单击一个按钮来“同意”他们的“责任免责声明”(这确实很有趣!)。我借助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
P.S。下一页的谜语也可以用相同的方式解决。 :)