【问题标题】:How do I extract data from a script?如何从脚本中提取数据?
【发布时间】:2013-07-24 15:48:08
【问题描述】:

这是包含所需数据的脚本在 html 页面上的样子:

u'{ displayName:"iPhone 5 16GB 黑色", productNameUrl:"apple-iphone-5-16gb-black-and-slate", _default:“true”,优先级:“1”,paymMinPrice:“9.99”,paymMinMrc:“46.00”, paymMinContractLength:"24 个月", pagmMinPrice:"", paygMinMrc:"", paygMinContractLength:"", paymentTypeUrl:"pay-monthly" } handset.imgURLimage_reg_url //handset.contactless 非接触式手机。颜色颜色 手机比较 比较手机 独家 独家 手机.dmMessage dmMessage 手机.ctaButton ctaButton ifhsetDispArr["iphone-5"] !空{handDispObj hsetDispArr["iphone-5"] handDispObj.handsets.pushhandset } 其他 { 手机.pushhandset handDispObj {productDispPFUrl:"iphone-5-group", productPFUrl:"apple-iphone-5-16gb-black-and-slate", 制造商名称:“Apple”,productDispName:“iPhone 5”, 手机:手机} 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。

【问题讨论】:

  • 它在文件中吗? grep 呢?
  • 没有此信息在我在该 html 页面上进行一些工作后创建的变量中。
  • 我希望我的代码在此变量中查找 productNameUrl 标记,并在本例中提取该标记下的任何信息,即 apple-iphone-5-16gb-black-and-slate

标签: javascript python html python-2.7


【解决方案1】:

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

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

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

全部只使用拆分功能

【讨论】:

    【解决方案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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-23
      • 2020-06-18
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多