I want to extract data from a json that is defined in a javascript variable. This looks like this:
...HTML CODE... <script> var myvar = { name: "John", lastname: "Doe" } </script> ...HTML CODE...
I need to get: John Doe
Can I do it using Xidel?
Anonymous
You seem to have CSS turned off. Please don't fill out this field.
yes, but the script is treated as plaintext and you need to get the text with the object first. e.g.
json(substring-after(//script, "myvar ="))/(name,lastname)
or
json(extract(//script, "myvar *= *(\{.*\})", 1, "s"))/(name,lastname)
or in xidel 0.9.9:
json(substring-after(//script, "myvar ="))?*
Thanks Benito, it works perfectly!
The command works fine, but when there is a script tag before it doesn't work. How do I specify the script tag I want to get?
I also want to save the data in variables, but I seem to be doing something wrong. I'm using this command:
for /f "delims=" %%a in ('xidel "page.html" -e "name:=json(substring-after(//script, \"myvar =\"))/(name)" -e "lastname:=json(substring-after(//script, \"myvar =\"))/(lastname)" --output-format cmd') do %%a
Seeing you're on Windows that's because using \" inside double quotes will get you into lots of trouble. Use single quotes:
\"
FOR /F "delims=" %%A IN ('xidel -s "page.html" -e "name:=json(substring-after (//script,'myvar ='))/name" -e "lastname:=json(substring-after(//script,'myva r ='))/lastname" --output-format^=cmd') DO %%A
You can use 1 query to export both variables btw:
FOR /F "delims=" %%A IN ('xidel -s "page.html" -e "json(substring-after(//scr ipt,'myvar ='))/(name:=name,lastname:=lastname)" --output-format^=cmd') DO %% A
You could even have "John" and "Doe" automatically being assigned a variable with the corresponding attribute name:
FOR /F "delims=" %%A IN ('xidel -s "page.html" --extract-exclude=json -e "jso n:=json(substring-after(//script,'myvar =')),$json() ! eval(x'{.}:=$json/{.}' )" --output-format^=cmd') DO %%A
I want to extract data from a json that is defined in a javascript variable. This looks like this:
I need to get:
John
Doe
Can I do it using Xidel?
yes, but the script is treated as plaintext and you need to get the text with the object first. e.g.
or
or in xidel 0.9.9:
Last edit: Benito van der Zander 2019-03-18
Thanks Benito, it works perfectly!
The command works fine, but when there is a script tag before it doesn't work. How do I specify the script tag I want to get?
I also want to save the data in variables, but I seem to be doing something wrong. I'm using this command:
for /f "delims=" %%a in ('xidel "page.html" -e "name:=json(substring-after(//script, \"myvar =\"))/(name)" -e "lastname:=json(substring-after(//script, \"myvar =\"))/(lastname)" --output-format cmd') do %%a
Seeing you're on Windows that's because using
\"
inside double quotes will get you into lots of trouble. Use single quotes:You can use 1 query to export both variables btw:
You could even have "John" and "Doe" automatically being assigned a variable with the corresponding attribute name:
Last edit: Reino 2019-08-03