Issue
I would like to get the value of uuid
key in the below text. Can I achieve this with XPATH
?
I am scraping this value from a website source code.
jQuery(document).ready(function ($) {
var infoTemplate = Handlebars.compile($('#vehicle-person-template').html());
var summaryTemplate = Handlebars.compile($('#vehicle-summary-template').html());
axios.post("https:\/\/www.merinfo.se\/ajax\/vehicles", {
uuid: "21ac0674-488a-11e8-9b40-47e7b0ba95bc" })
.then(function (response) {
$('#vehicle-info').html(infoTemplate(response.data.data));
$('#vehicle-summary').html(summaryTemplate(response.data.data));
})
.catch(function (error) {
$("#vehicle-info").html('<p class="font-italic mb-0">Fordonsinformation ej tillgängligt för tillfället</p>');
$('#vehicle-summary').html('<p class="font-italic mb-0">Fordonsinformation ej tillgängligt för tillfället</p>');
});
});
jQuery(document).ready(function ($) {
var source = $("#person-residence-template").html();
var template = Handlebars.compile(source);
axios.post("https:\/\/www.merinfo.se\/api\/v1\/person\/residence", {
uuid: "21ac0674-488a-11e8-9b40-47e7b0ba95bc" })
.then(function (response) {
if (typeof response.data.data !== 'undefined') {
$('#residence-info').html(template(response.data.data));
} else {
$("#residence-info").html('<p class="font-italic mb-0">Vi saknar bostadsinformation för Björn</p>');
}
})
.catch(function (error) {
$("#residence-info").html('<p class="font-italic mb-0">Vi saknar bostadsinformation för Björn</p>');
});
});
Solution
If you have the JS code as text you could use regular expressions to get that value.
Code
import re
pattern = r'uuid:\s\"(.*?)\"'
uuids = re.findall(pattern, code_text)
Assuming you have the code in the code_text
variable.
uuids
is a list with all the uuids in the code.
Pattern explanation
uuid:
: The literal text 'uuid:'\s
: Followed by a space\"
: Then the open quotes(.*?)
: Any characters (and making a group with this characters, this is the value you want)\"
: Then the closing quotes
The ?
after the .*
is for stopping matching any character if a "
is encounter. If you don't put this ?
then it would match until the last "
of the code.
The (
and )
creates a group, and findall
will give as result all groups values in a list, in this case, all inside the quotes, the value you want.
Answered By - Jorge Morgado
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.