I have a PHP file that responds with a similar javascript response to below.
var count = 0;
var actors = [];
var directors = [];
var movies = ["7/n/n# L<DxJ", "7x/nx/nxlNTv", "(N/lN/lNUnNTv", "}x/Yx/YxL#llNTv", "}/Y/Y<L<J", "O/u;uiLY<J", "@/J/JNUL<DxJ"];
var hidden = [];
var r_space = null;
var chars = {
"i": "N",
"R": "6",
"u": "T",
"a": "i",
"V": "p",
">": "a"
};
for (var i in chars) {
if (i == ' ') r_space = asta[i];
}
The movie names are encoded using a simple algorithm using var chars. I want to iterate through the var movies and get all values in to an array so I can decode them. I tried using below code to split by ";" and then call [3] to get var movies and then split it by "," to get each movie name and store it an array but the issues is some words in var movies also contain ";".
var_array = str(response.content.decode()).split(";")[3][14:-1].split(",")
for va in var_array:
print(va[1:-1])
Is there a different way I can get all values in var movies to an array and then decode? Thanks.
CodePudding user response:
You can use a combination of json
and re
(regex) modules:
json.loads(re.search("var movies = (?P<movies>\[. \]);", str(response.content.decode())).group("movies"))
This will result in:
['7\u200b/n\u200b/n# L<DxJ',
'7x\u200b/nx\u200b/nxlNTv',
'(N\u200b/lN\u200b/lNUnNTv',
'}x\u200b/Yx\u200b/YxL#llNTv',
'}\u200b/Y\u200b/Y<L<J',
'O\u200b/u\u200b;uiLY<J',
'@\u200b/J\u200b/JNUL<DxJ']
Then you can simply run a for loop over this list.
CodePudding user response:
try to Use the JavaScript function JSON.parse() to convert text into a JavaScript object and then loop on it to get movies value.
let var_array = JSON.parse(movies);
for va in var_array:
console.log(va)