[英]python lxml xpath get the nodes attributes with specific string pattern
我正在學習xpath並嘗試使用python lxml / html獲取具有特定節點屬性的節點的值,例如(google playstore)。 從下面的代碼中,我想從節點“ a”獲得以“ mailto:”開頭的屬性“ href”的開發人員電子郵件值。 我的python代碼段返回了應用名稱,但開發人員電子郵件為空。 謝謝
<html>
<div class="id-app-title" tabindex="0">Candy Crush Saga</div>
<div class="meta-info meta-info-wide">
<div class="title"> Developer </div>
<a class="dev-link" href="https://www.google.com/url?q=http://candycrush.com" rel="nofollow" target="_blank"> Visit website </a>
<a class="dev-link" href="mailto:candycrush@kingping.com"
rel="nofollow" target="_blank">candycrush@kingping.com </a> ##Interesting part here
</div>
</html>
def get_app_from_link(self,link):
start_page=requests.get(link)
#print start_page.text
tree = html.fromstring(start_page.text)
name = tree.xpath('//div[@class="id-app-title"]/text()')[0]
#developer=tree.xpath('//div[@class="dev-link"]//*/div/@href')
developer=tree.xpath('//div[contains(@href,"mailto") and @class="dev-link"]/text()')
print name,developer
return
現在,您使用的標簽div
,而不是a
:
'//a[contains(@href,"mailto") and @class="dev-link"]/text()'
另外,您的函數不會返回任何項目。 使用return
這樣的:
def get_app_from_link(self,link)::
# your code
return name, developer
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.