如何从脚本中提取数据?

时间:2013-07-24 07:23:56

标签: javascript python html python-2.7

这是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  {}'

我需要从此脚本中的productNameUrl(顶部的第二个标记)标记下提取信息。有人可以告诉我我该怎么做?

由于此脚本不是标准JSON格式,因此我也无法使用JSON.load。

2 个答案:

答案 0 :(得分:2)

假设您的信息字符串存储在“info”

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

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

全部仅使用分割功能

答案 1 :(得分:1)

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]