使用Python XPath表达式提取文本

时间:2017-03-24 17:50:31

标签: python xpath scrapy

我想显示http:///gb/groceries/easter-essentials--%28approx-205kg%29

scrapy中我使用了这个XPath表达式:

response.xpath('//div[@class="productNameAndPromotions"]/h3/a/href').extract()

但它不起作用!

<div class="product ">
    <div class="productInfo">
        <div class="productNameAndPromotions">
            <h3>
                <a href="http:///gb/groceries/easter-essentials--%28approx-205kg%29">
                    <img src="http:co.uk/wcsstore7.20.1.145/ExtendedSitesCatalogAssetStore/image/catalog/productImages/08/020000008_L.jpeg" alt="" />
                </a>
            </h3>
        </div>        
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

import java.util.*; public class PalPrime { public boolean prime(int n) { int c=0; for(int i=1;i<=n;i++) { if(n%i==0) c++; } if(c==2) return true; else return false; } public boolean palindrome(int n) { int rev=0,temp=n; while(temp!=0) { rev=rev*10+(temp%10); temp=temp/10; } if(rev==n) return true; else return false; } public static void main(String args[]) { Scanner ob=new Scanner(System.in); System.out.println("Enter number to be checked"); int num=ob.nextInt(); PalPrime obj=new PalPrime(); if(obj.prime(num)==true && obj.palindrome(num)==true) System.out.println(num+" is a Prime Palindrome i.e. a PalPrime Number"); else System.out.println(num+" is not a PalPrime Number"); } } 表示您希望获取.with_entities(VeryWideTable.a, VeryWideTable.b, literal("hello")) 的子元素//div[@class="productNameAndPromotions"]/h3/a/href

如果要提取节点&#39;属性,例如href,您需要使用a语法。请尝试以下:

href
相关问题