如何从脚本中提取数据?

2024-06-02 17:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这是包含所需数据的脚本在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.loads


Tags: and脚本truedefaultapplereturnelseblack
2条回答
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]

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

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

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

全部只使用split函数

相关问题 更多 >