简体   繁体   English

使用 PHP (正则表达式)从抓取的 HTML 页面中提取 Javascript 变量

[英]Extract Javascript Variables from Scraped HTML Page using PHP (Regex)

I'm stuggling to extract Javascript variables from a scraped webpage's HTML dump.我正在努力从抓取网页的 HTML 转储中提取 Javascript 变量。

Currently using this regex目前使用这个正则表达式

    $re = '/window\.universal_variable\s*=\s*\{(.*?)\}/ms';

but it only shows me the first set of values.但它只向我展示了第一组值。 I'm basically trying to get all variables and values that are under product (ie id, product_id, sku, etc)我基本上是在尝试获取产品下的所有变量和值(即 id、product_id、sku 等)

    <script type="text/javascript">
window.universal_variable = {
page: {
category: "product" ,
searchTerm: "sony",
environment: "production",
variation: "production",
revision: "1.1"
},
user: {
otb: "",
ATG_FO_IND: "A",\t
ooops_preference: "false",
registered_today: false,
registration_date: "",
registered_in_current_session: false,\tidv_verified: true,
last_order_date: "",
start_date: "",
first_order: false,\treturning: false,
last_transaction_payment_type: "",
unicaSegment: "",
targetedPromos :"",
cva:"0",
cvb:"1", 
cvc:""
}// end of user\t
,\t
product:{
id: "KEN6C",
product_id: "prod1086433641",
sku: "KEN6C",
manufacturer: "",
category: "Televisions",
category_facet: "4740",
department: "Electricals",
subcategory: "electricals_televisions",
currency: "GBP",
unit_price: "",
unit_sale_price: "319.0",
rating: "4.3",
ratingCount: "2048"
}// end of product
}// end of window.universal_variable\t
window.sdgGA = {
environment: "production",
device: "desktop",
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
currency: "GBP",
page: {
PID: "test : PRODUCT",
loggedInState: "not logged in",
category:"product",
customerStatus: "new"
},

</script>

Any suggestions?有什么建议么?

Rather than trying to do it with a regex, which would be extremely brittle, I would suggest using a transpiler such as this one .与其尝试使用非常脆弱的正则表达式,我建议使用这样的转译器。 I tested it on your example code and it worked great.我在您的示例代码上对其进行了测试,效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM