2016-05-15 119 views
0

我试图提取类似的应用程序从谷歌Play商店中从这里的链接(使用XPath)XPath来提取链接或HREF中

https://play.google.com/store/apps/details?id=com.mojang.minecraftpe 

下面是我想的链接(标记为绿色)的屏幕截图提取 enter image description here

HTML样品

<div class="details"> 
    <a href="/store/apps/details?id=com.imangi.templerun" class="card-click-target"></a> 
    <a title="Temple Run" href="/store/apps/details?id=com.imangi.templerun" class="title">Temple Run 
    <span class="paragraph-end"/> 
    </a> 
    <div>....</div> 
    <div>....</div> 
</div> 

我用下面的XPath chrome console找到一个链接,但它做esnt返回标签的href属性。但对于其他属性(例如“标题”)起作用。

下面的XPath不工作(摘录的 “href”)

//*[@id="body-content"]/div/div/div[2]/div[1]//*/a[2]/@href 

下面XPath的工作原理(摘录 “称号”)

//*[@id="body-content"]/div/div/div[2]/div[1]//*/a[2]/@title 

enter image description here

Python代码

回答

0

HTML的链接页面右侧的各个图块的格式如下*:

<div class="details"> 
    <a href="/store/apps/details?id=com.imangi.templerun" class="card-click-target"></a> 
    <a title="Temple Run" href="/store/apps/details?id=com.imangi.templerun" class="title">Temple Run 
    <span class="paragraph-end"/> 
    </a> 
    <div>....</div> 
    <div>....</div> 
</div> 

原来,<a>class="title"元素唯一识别你的目标在页面<a>元素。

//a[@class="title"]/@href 

无论如何,你注意到这个问题似乎是具体到Chrome XPath计算器**:那么作为的XPath可以很简单。既然你提到的有关Python,简单的Python代码证明了XPath的应该只是罚款:

>>> from urllib2 import urlopen 
>>> from lxml import html 
>>> req = urlopen('https://play.google.com/store/apps/details?id=com.mojang.minecraftpe') 
>>> raw = req.read() 
>>> root = html.fromstring(raw) 
>>> [h for h in root.xpath("//a[@class='title']/@href")] 
['/store/apps/details?id=com.imangi.templerun', '/store/apps/details?id=com.lego.superheroes.dccomicsteamup', '/store/apps/details?id=com.turner.freefurall', '/store/apps/details?id=com.mtvn.Nickelodeon.GameOn', '/store/apps/details?id=com.disney.disneycrossyroad_goo', '/store/apps/details?id=com.rovio.angrybirdsstarwars.ads.iap', '/store/apps/details?id=com.rovio.angrybirdstransformers', '/store/apps/details?id=com.disney.dinostampede_goo', '/store/apps/details?id=com.turner.atskisafari', '/store/apps/details?id=com.moose.shopville', '/store/apps/details?id=com.DisneyDigitalBooks.SevenDMineTrain', '/store/apps/details?id=com.turner.copatoon', '/store/apps/details?id=com.turner.wbb2016', '/store/apps/details?id=com.tov.google.ben10Xenodrome', '/store/apps/details?id=com.turner.ggl.gumballrainbowruckus', '/store/apps/details?id=com.lego.starwars.theyodachronicles', '/store/apps/details?id=com.mojang.scrolls'] 

*)精简版。您可以将其作为提供最小HTML样本的示例。

**)我可以重现此问题,@href s在我的Chrome控制台中打印为空字符串。同样的问题也发生在其他人身上:Chrome element inspector Xpath with @href won't show link text