2017-03-21 45 views
-4

我需要一些帮助来从xpath中用xpath提取部分信息!我想提取审查的评级 为例:这是HTML代码如何在python scrapy中用Xpath提取信息

<div id="BVRRRatingOverall_Rating_Summary_1" class="BVRRRating BVRRRatingNormal BVRRRatingOverall"> 
<div class="BVRRLabel BVRRRatingNormalLabel">Overall rating&nbsp;</div> 
<div class="BVRRRatingNormalImage"><img src="/4_9/5/rating.gif" class="BVImgOrSprite" alt="4.9 out of 5" title="4.9 out of 5" width="85" height="16"></div> 
<span itemprop="aggregateRating" itemscope="" itemtype="http://schema.org/AggregateRating" class="BVRRRatingNormalOutOf"> 
     <span itemprop="ratingValue" class="BVRRNumber BVRRRatingNumber">4.9</span> 
     <span class="BVRRSeparatorText">out of </span> 
     <span itemprop="bestRating" class="BVRRNumber BVRRRatingRangeNumber">5</span> 
     <meta itemprop="reviewCount" content="23"> 
    </span> 
</div> 

从这个代码,我想显示“4.9总分5”或3个变量包含“4.9”,“出”,“5”

我用这一点,但没有奏效:

response.xpath('//div[@class="BVRRRatingNormalImage"]/img/@alt').extract() 

任何帮助吗?

+3

股利不具备类“BVRRRatingNormalImage”。 – betseyb

+0

抱歉betseyb!我只是把下面的HTML代码! –

+0

对于这个html例子应该这个xpath表达式: '// img [@ class =“BVImgOrSprite”]/@ alt'。 但没有网站链接和你的代码的一些例子,我不知道它是否会为你工作。 – vold

回答

0
response.xpath('//span[@itemprop="aggregateRating"]/span/text()').extract() 

它会给以列表形式三元

response.xpath('//span[@itemprop="ratingValue"]/text()').extract_first() 

它会给只有一个元素为字符串“4.9”

+0

非常感谢您的答案pythonlearner!我尝试了第二个路径它的工作,但第一显示什么都没有 –

+0

页可能会更新@ghiles bouzembrak,如果它的工作,您可以接受答案 – pythonlearner

+0

答案接受!谢谢 –