如何使用scrapy在网站中选择select标签的选项?
我正在学习如何使用Scrapy。最近我在跟着一个教程,学习如何模拟用户登录,然后在成功登录后收集数据(下面有代码)。不过,如果在登录过程中有一个下拉列表,我该怎么编写代码让Scrapy选择其中的某个选项呢?
class LoginSpider(BaseSpider):
name = 'example.com'
start_urls = ['http://www.example.com/users/login.php']
def parse(self, response):
return [FormRequest.from_response(response,
formdata={'username': 'john', 'password': 'secret'},
callback=self.after_login)]
def after_login(self, response):
# check login succeed before going on
if "authentication failed" in response.body:
self.log("Login failed", level=log.ERROR)
return
# continue scraping with authenticated session...
选择标签:
<select>
<option>Value 1</option>
<option>Value 2</option>
<option>Value 3</option>
</select>
我该如何编写代码,让Scrapy在输入登录信息的同时,从下拉列表中选择,比如说选择“值1”?
1 个回答
-1
你可能需要把选项和它的值作为一个键放在表单数据里,比如 {Value,1} 或 {Value,2}。