Home > Software design >  How to use Scarpy Selector to get a value related with an id?
How to use Scarpy Selector to get a value related with an id?

Time:10-12

Following the html source code example:

<div id="form1">
    <input type="hidden" name="env_x" id="env_id_x" value="Value1">                             
    <input type="hidden" name="env_y" id="env_id_y" value="Value2">
    <input type="hidden" name="env_z" id="env_id_z" value="Value3">
    <input type="hidden" name="env_w" id="env_id_w" value="Value4">

If I want to get the value associated with a certain id how should I do it without having to iterate over all the inputs?

r = response.css('div[id="form1"]').css(input::attr(id)).getall()
# ["env_id_x","env_id_y","env_id_z","env_id_w"]

Now what i want is the value associated with the id="env_id_z" -> Value3

Kind Regards,

João

CodePudding user response:

Try:

r = response.css('div[id="form1"]').css('input#env_id_z::attr(value)').get()
  • Related