本文整理匯總了Python中util.ElementHelper.get_xpath_by_element方法的典型用法代碼示例。如果您正苦於以下問題:Python ElementHelper.get_xpath_by_element方法的具體用法?Python ElementHelper.get_xpath_by_element怎麽用?Python ElementHelper.get_xpath_by_element使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類util.ElementHelper
的用法示例。
在下文中一共展示了ElementHelper.get_xpath_by_element方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: print_cluster_record
# 需要導入模塊: from util import ElementHelper [as 別名]
# 或者: from util.ElementHelper import get_xpath_by_element [as 別名]
def print_cluster_record(cls, clusters, doctree):
ElementHelper.print_element(doctree)
for cluster in clusters:
if len(cluster)>1:
print '===='*10
nodes = clusters[cluster]
for node in nodes:
print ElementHelper.get_xpath_by_element(node, doctree), node.get(py)
示例2: clean_body
# 需要導入模塊: from util import ElementHelper [as 別名]
# 或者: from util.ElementHelper import get_xpath_by_element [as 別名]
def clean_body(clusters, doctree, title_node=None, debug = False):
#filter user comments and all link records
clusters = filter_cluster(clusters)
if len(clusters) == 0:
title = ElementHelper.element_text_content(title_node)
return title, title
#choose cluster which has most texts
maxCluster = get_biggest_cluster(clusters)
nodes = clusters[maxCluster]
# m = {}
# m[maxCluster] = nodes
# filter_cluster(m)
#get all children of max cluster record
allnodes = []
for node in nodes:
children = ElementHelper.get_children(node)
allnodes.extend(children)
#sort max cluster nodes by its preorder index
allnodes.sort(lambda p,q:ElementHelper.get_element_preorder_num(p)-ElementHelper.get_element_preorder_num(q))
# debug=True
if debug:
print 'maxCluster: %s' % maxCluster
for n in allnodes:
print ElementHelper.get_xpath_by_element(n, doctree), n.get(py)
s = ElementHelper.get_element_preorder_num(allnodes[0])
t = ElementHelper.get_element_preorder_num(allnodes[-1])
#correct start position by title node
title_text = ''
#====================================================================
#在實驗時借助title糾正正文文本的起始位置可以提高recall,但是實際過程中不需要
if title_node is not None:
index = ElementHelper.get_element_preorder_num(title_node)
if index < s:
s = index #ToDo:add 2016/03/09
title_text = ElementHelper.element_text_content(title_node)
#====================================================================
body = ElementHelper.get_body(doctree)
# remove nodes which not belong to main text
set_text_mark(body, s, t)
remove_nontext_element(body)
return body, title_text
示例3: find_first_sibling_record_node
# 需要導入模塊: from util import ElementHelper [as 別名]
# 或者: from util.ElementHelper import get_xpath_by_element [as 別名]
def find_first_sibling_record_node(cls, element, doctree):
'''找到element所在區域的起始節點
1.首選查看element的xpath下表,如果其下表<2,表示element左邊沒有兄弟節點了,直接返回element
2.如果element的xpath下標大於=2,表示element左邊有兄弟節點,那麽先找到element的父親(以便於訪問element的兄弟節點,然後index=設置為element的下表-2
查看parentt[index]是否是數據區域,如果是繼續項左尋找,否則返回parent[index+1]
'''
parent = element.getparent()
if len(parent)<2:
return element
element_xpath = ElementHelper.get_xpath_by_element(element, doctree)
# print 'xpath: %s' %element_xpath
element_last_index = StringHelper.get_digits(element_xpath.split('/')[-1])
if element_last_index < 2:
return element
index = element_last_index - 2
# print 'parent length:%d' %len(parent)
while index >= 0:
# print index
if parent[index].get(kg_record_mark) == '1':
index -= 1
else:
break
return parent[index+1]
示例4: get_article_wish
# 需要導入模塊: from util import ElementHelper [as 別名]
# 或者: from util.ElementHelper import get_xpath_by_element [as 別名]
def get_article_wish(clusters, doctree, title_node=None, debug = False):
#filter
clusters = filter_cluster(clusters)
if len(clusters) == 0:
return ElementHelper.element_text_content(title_node)
maxCluster = get_biggest_cluster(clusters)
nodes = clusters[maxCluster]
m = {}
m[maxCluster] = nodes
filter_cluster(m)
#============================================
if debug:
# for test
print 'maxCluster: %s' %maxCluster
for n in nodes:
print ElementHelper.get_xpath_by_element(n, doctree)
allnodes = []
for node in nodes:
children = ElementHelper.get_children(node)
allnodes.extend(children)
allnodes.sort(lambda p,q:ElementHelper.get_element_preorder_num(p)-ElementHelper.get_element_preorder_num(q))
s = ElementHelper.get_element_preorder_num(allnodes[0])
t = ElementHelper.get_element_preorder_num(allnodes[-1])
title_text = ''
if title_node is not None:
index = ElementHelper.get_element_preorder_num(title_node)
if index < s:
s = index
title_text = ElementHelper.element_text_content(title_node)
body = ElementHelper.get_body(doctree)
set_text_mark(body, s, t)
remove_nontext_element(body)
# ElementHelper.print_element(body)
return ElementHelper.element_text_content_list(body), title_text
示例5: find_first_sibling_record_node
# 需要導入模塊: from util import ElementHelper [as 別名]
# 或者: from util.ElementHelper import get_xpath_by_element [as 別名]
def find_first_sibling_record_node(cls, element, doctree):
parent = element.getparent()
if len(parent)<2:
return element
element_xpath = ElementHelper.get_xpath_by_element(element, doctree)
# print 'xpath: %s' %element_xpath
element_last_index = StringHelper.get_digits(element_xpath.split('/')[-1])
if element_last_index < 2:
return element
index = element_last_index - 2
# print 'parent length:%d' %len(parent)
while index >= 0:
# print index
if parent[index].get(kg_record_mark) == '1':
index -= 1
else:
break
return parent[index+1]