Skip to main content
 首页 » 编程设计

xpath之如何使用xpath检索JavaScript变量值=

2025年05月04日12lovecherry

我正在尝试从此JS代码中提取价格和其他属性:

  <script type="application/ld+json"> 
{ 
  "@context": "http://schema.org/", 
  "@type": "Product", 
  "name": "Rolex Cellini Time 50505", 
  "image": [ 
        "https://chronexttime.imgix.net/S/1/S1006/S1006_58774a90efd04.jpg?w=1024&amp;auto=format&amp;fm=jpg&amp;q=75&amp;usm=30&amp;usmrad=1&amp;h=1024&amp;fit=clamp"      ], 
  "description": "Werk: automatic; Herrenuhr; Gehäusegröße: 39; Gehäuse: rose-gold; Armband: leather; Glas: sapphire; Jahr: 2018; Lieferumfang: Originale Box, Originale Papiere, Herstellergarantie", 
  "mpn": "S1006", 
  "brand":{ 
    "@type": "Thing", 
    "name": "Rolex" 
  }, 
  "offers":{ 
    "@type": "Offer", 
    "priceCurrency": "EUR", 
    "price": "11500", 
    "itemCondition": "http://schema.org/NewCondition", 
    "availability": "http://schema.org/InStock", 
 
    "seller":{ 
      "@type": "Organization", 
      "name": "CHRONEXT Service Germany GmbH" 
    } 
  } 
} 
</script> 


另外,此代码也可以做到这一点:

  <script type="text/javascript"> 
window.articleInfo = { 
    'id': 'S1006', 
    'model': 'Cellini Time', 
    'brand': 'Rolex', 
    'reference': '50505', 
    'priceLocal': '11500', 
    'currencyCode': 'EUR' 
}; 


同一页面上还有更多其他JS代码,因此我不确定如何使用xpath处理该特定脚本。

我尝试了这个:

response.xpath('//script[contains(.,"price")]/text()').extract_first() 


但响应中包含很多值,而我只寻找11500的价格。稍后,我也将尝试获取例如名称和条件。

请您参考如下方法:

对于第一个脚本,是的,没有比直接使用json解码更好的选择了。

对于第二个,当然您总是可以使用正则表达式,但是我建议使用一种更干净,更好的解决方案,将使用js2xml将javascript转换为xpath可查询格式:

$ pip安装js2xml

假设一个脚本具有以下结构:

<script type="text/javascript"> 
window.articleInfo = { 
    'id': 'S1006', 
    'model': 'Cellini Time', 
    'brand': 'Rolex', 
    'reference': '50505', 
    'priceLocal': '11500', 
    'currencyCode': 'EUR' 
}; 
</script> 


格式化如下:

import js2xml 
 
... 
 
parsed = js2xml.parse(response.xpath('//script/text()').extract_first()) 


您可以看到 parsed的结构:

>> print(js2xml.pretty_print(parsed)) 
>> <program> 
  <assign operator="="> 
    <left> 
      <dotaccessor> 
        <object> 
          <identifier name="window"/> 
        </object> 
        <property> 
          <identifier name="articleInfo"/> 
        </property> 
      </dotaccessor> 
    </left> 
    <right> 
      <object> 
        <property name="id"> 
          <string>S1006</string> 
        </property> 
        <property name="model"> 
          <string>Cellini Time</string> 
        </property> 
        <property name="brand"> 
          <string>Rolex</string> 
        </property> 
        <property name="reference"> 
          <string>50505</string> 
        </property> 
        <property name="priceLocal"> 
          <string>11500</string> 
        </property> 
        <property name="currencyCode"> 
          <string>EUR</string> 
        </property> 
      </object> 
    </right> 
  </assign> 
</program> 


这意味着您现在可以像这样获得所需的信息:

parsed.xpath('//property[@name="id"]/string/text()')[0] 
parsed.xpath('//property[@name="model"]/string/text()')[0] 
parsed.xpath('//property[@name="brand"]/string/text()')[0] 
... 


希望我能帮到你。