0

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>');
                });
        });
    
4
  • Have you consider using regular expressions? Commented May 24, 2021 at 17:10
  • I have no understanding of Regular Expression. Commented May 24, 2021 at 17:13
  • I will live you an answer using it and explained Commented May 24, 2021 at 17:14
  • 1
    That's so nice of you... Commented May 24, 2021 at 17:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You can check it here
Good to know :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.