簡體   English   中英

使用 PHP (正則表達式)從抓取的 HTML 頁面中提取 Javascript 變量

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

我正在努力從抓取網頁的 HTML 轉儲中提取 Javascript 變量。

目前使用這個正則表達式

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

但它只向我展示了第一組值。 我基本上是在嘗試獲取產品下的所有變量和值(即 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>

有什么建議么?

與其嘗試使用非常脆弱的正則表達式,我建議使用這樣的轉譯器。 我在您的示例代碼上對其進行了測試,效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM