简体   繁体   English

如何从脚本中提取数据?

[英]How do I extract data from a script?

This is how script containing required data looks like on html page: 这是包含所需数据的脚本在html页面上的样子:

u'{                    displayName:"iPhone 5 16GB Black",             
 productNameUrl:"apple-iphone-5-16gb-black-and-slate",                 
 _default:"true",                     priority:"1",                     paymMinPrice:"9.99",                     paymMinMrc:"46.00",          
 paymMinContractLength:"24 Months",                    
 pagmMinPrice:"",                     paygMinMrc:"",                   
 paygMinContractLength:"",                    
 paymentTypeUrl:"pay-monthly"                }           
 handset.imgURLimage_reg_url            //handset.contactless 
 contactless            handset.colours  colours           
 handset.compare  compare            handset.exclusive  exclusive      
 handset.dmMessage  dmMessage            handset.ctaButton  ctaButton  
 ifhsetDispArr["iphone-5"] ! null {                handDispObj 
 hsetDispArr["iphone-5"]               
 handDispObj.handsets.pushhandset            } else {               
 handsets.pushhandset                handDispObj 
 {productDispPFUrl:"iphone-5-group",
 productPFUrl:"apple-iphone-5-16gb-black-and-slate",
 manufacturerName:"Apple", productDispName:"iPhone 5",
 handsets:handsets}                hsetDispKeys.push"iphone-5"         
 }            handDispObj.handsets.sortfunctionh1,h2{ifh1._default 
 "true"{return false}else ifh2._default  "true"{return true}else{return
 h1.priority > h2.priority}}            hsetDispArr["iphone-5"] 
 handDispObj                   var handDispObj  {}'

I need to extract information under productNameUrl (2nd Tag from the top) tag from this script. 我需要从此脚本的productNameUrl (顶部的第二个标签)标签下提取信息。 Can someone please tell me how do I do it? 有人可以告诉我该怎么做吗?

As this script is not in standard JSON format I can't use JSON.loads also. 由于此脚本不是标准JSON格式,因此我也不能使用JSON.loads。

Let's assume your information string is stored in "info" 假设您的信息字符串存储在“ info”中

var productNameUrl = info.split('productNameUrl:"')[1];

ProductNameUrl = productNameUrl.split('"')[0];

All only using the split function 全部仅使用分割功能

u_str = u'{ displayName:"iPhone 5 16GB Black", productNameUrl:"apple-iphone-5-16gb-black-and-slate",_default:"true", priority:"1", paymMinPrice:"9.99", paymMinMrc:"46.00",paymMinContractLength:"24 Months",pagmMinPrice:"", paygMinMrc:"",paygMinContractLength:"",paymentTypeUrl:"pay-monthly" }handset.imgURLimage_reg_url //handset.contactless contactless handset.colours colourshandset.compare compare handset.exclusive exclusivehandset.dmMessage dmMessage handset.ctaButton ctaButtonifhsetDispArr["iphone-5"] ! null { handDispObj hsetDispArr["iphone-5"]handDispObj.handsets.pushhandset } else {handsets.pushhandset handDispObj {productDispPFUrl:"iphone-5-group", productPFUrl:"apple-iphone-5-16gb-black-and-slate", manufacturerName:"Apple", productDispName:"iPhone 5", handsets:handsets} hsetDispKeys.push"iphone-5"} handDispObj.handsets.sortfunctionh1,h2{ifh1._default "true"{return false}else ifh2._default "true"{return true}else{return h1.priority > h2.priority}} hsetDispArr["iphone-5"] handDispObj var handDispObj {}'

str_to_find = 'productNameUrl'
p1 = u_str.find(str_to_find)+len(str_to_find)+1
p2 = u_str.find('",', u_str.find(str_to_find))+1
print u_str[p1:p2]

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

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