从 JavaScript 代码字符串中抓取特定文本
Scrape a specific text from a JavaScript code string
我想在下面的文本中获取 uuid
键的值。我可以用 XPATH
实现吗?
我正在从网站源代码中抓取这个值。
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>');
});
});
如果您将 JS 代码作为文本,您可以使用正则表达式来获取该值。
代码
import re
pattern = r'uuid:\s\"(.*?)\"'
uuids = re.findall(pattern, code_text)
假设您在 code_text
变量中有代码。
uuids
是包含代码中所有 uuid 的列表。
图案说明
uuid:
: 文字 'uuid:'
\s
:后跟一个 space
\"
:然后是左引号
(.*?)
: 任意字符(和这个字符组成一个组,这就是你想要的值)
\"
:然后是结束引号
.*
后面的?
是为了在遇到"
时停止匹配任何字符。如果你不放这个 ?
那么它将匹配到代码的最后 "
。
(
和 )
创建一个组,findall
将作为结果给出列表中的所有组值,在这种情况下,所有在引号内,值你想要。
我想在下面的文本中获取 uuid
键的值。我可以用 XPATH
实现吗?
我正在从网站源代码中抓取这个值。
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>');
});
});
如果您将 JS 代码作为文本,您可以使用正则表达式来获取该值。
代码
import re
pattern = r'uuid:\s\"(.*?)\"'
uuids = re.findall(pattern, code_text)
假设您在 code_text
变量中有代码。
uuids
是包含代码中所有 uuid 的列表。
图案说明
uuid:
: 文字 'uuid:'\s
:后跟一个 space\"
:然后是左引号(.*?)
: 任意字符(和这个字符组成一个组,这就是你想要的值)\"
:然后是结束引号
.*
后面的?
是为了在遇到"
时停止匹配任何字符。如果你不放这个 ?
那么它将匹配到代码的最后 "
。
(
和 )
创建一个组,findall
将作为结果给出列表中的所有组值,在这种情况下,所有在引号内,值你想要。