Python Goose extractor - "KNOWN_ARTICLE_CONTENT_TAGS " 流程似乎没有效果
Python Goose extractor - "KNOWN_ARTICLE_CONTENT_TAGS " flow doesn't seem to be effective
将 python goose2 用于 python 2.7 .
您将 tags/class 或要提取的文章的 ID 放在 KNOWN_ARTICLE_CONTENT_TAGS
中,似乎不起作用。
比如说,把里面的默认标签取为
KNOWN_ARTICLE_CONTENT_TAGS = [
{'attr': 'itemprop', 'value': 'articleBody'},
{'attr': 'class', 'value': 'post-content'},
{'tag': 'article'},
]
现在我的第一个问题是获取这些值的确切预期逻辑是什么?
- 是否认为这些文本中的所有文本都是默认文本?
- 它们只是作为对鹅的参考来增加这些节点内文本的分数但不保证所有内容都会显示吗?
- goose是否忽略其他常见的通用标签只考虑这些标签?
- 我看到如果 returns 为真,它会跳过添加兄弟姐妹,那是什么意思?
但经过一些调试,我发现提到的标签内的文本没有得到任何特殊的偏好,事实上,不调用已知的文章代码有完全相同的输出以及使用时某些来源的图像提取失败由于某种原因已知的标签。
进一步挖掘后,我还看到函数
def get_known_article_tags(self):
for item in KNOWN_ARTICLE_CONTENT_TAGS:
nodes = self.parser.getElementsByTag(
self.article.doc,
**item)
if len(nodes):
return nodes[0]
return None
在似乎没有任何标签的 article.doc
对象上操作。
在几乎所有帖子中也是如此 returns 只有带有文章标签的元素而不是带有属性的元素 itemprop = articleBody 即使文章中有它们。
调试后 is_articlebody
函数如下代码所示
def is_articlebody(self, node):
for item in KNOWN_ARTICLE_CONTENT_TAGS:
# attribute
if "attr" in item and "value" in item:
if(self.config.debug):
print 'for attr and value'
print self.parser.getAttribute(node, item['attr'])
print item['value']
print node
if self.parser.getAttribute(node, item['attr']) == item['value']:
if(self.config.debug):
print 'is article body from attribute'
return True
# tag
if "tag" in item:
print 'if tag'
print node.tag
if node.tag == item['tag']:
if(self.config.debug):
print 'is article body from tag'
return True
我注意到,即使目标提取文档中有 tags/class 类似的内容,此函数也不会返回 true。
print self.parser.getAttribute(node, item['attr'])
行始终返回为 null 。
如何让鹅获取已知列表attributes/classes/tags中提到的所有文本,就像上面的例子一样,我想获取多个里面的所有文本p 标签(也可以是 p 以外的其他标签)不管分数 ?
编辑:
在尝试进一步调试时,我意识到 get_known_articles_tags 函数只返回在字典中找到的第一个 tag/attribute,
关注:return nodes[0]
所以它只返回文档的那个单个节点,然后它只发送那个节点对象来找到顶级节点——假设该节点不满足 good/top 节点的条件,那么它 returns 为空,因此失败。
如何组合 nodes
列表中的所有节点对象,并将所有节点作为文档发送以解析并使用它来查找顶级节点?
我设法解决了与此问题相关的问题,
我更改了 return 语句的范围并传递了整个数组
def get_known_article_tags(self):
for item in KNOWN_ARTICLE_CONTENT_TAGS:
nodes = self.parser.getElementsByTag(
self.article.doc,
**item)
if len(nodes):
return nodes
return None
然后我将相同的节点数组一次传递给清洁器一个节点(在数组内)并将整个数组传递给 calculate_top_node
函数作为
self.article.top_node = self.extractor.calculate_best_node(doc)
然后在 nodes_to_check
函数中添加了一个额外的循环来检查数组的所有节点,
def nodes_to_check(self, docs):
"""\
returns a list of nodes we want to search
on like paragraphs and tables
"""
nodes_to_check = []
for doc in docs:
for tag in ['p', 'pre', 'td']:
items = self.parser.getElementsByTag(doc, tag=tag)
nodes_to_check += items
return nodes_to_check
这解决了 return 只有单个元素的问题。
我是通过查看 python 3 goose 代码逻辑得出这个结论的,该逻辑在 python2.7 语法上得到了更多的维护和实现。
将 python goose2 用于 python 2.7 .
您将 tags/class 或要提取的文章的 ID 放在 KNOWN_ARTICLE_CONTENT_TAGS
中,似乎不起作用。
比如说,把里面的默认标签取为
KNOWN_ARTICLE_CONTENT_TAGS = [
{'attr': 'itemprop', 'value': 'articleBody'},
{'attr': 'class', 'value': 'post-content'},
{'tag': 'article'},
]
现在我的第一个问题是获取这些值的确切预期逻辑是什么?
- 是否认为这些文本中的所有文本都是默认文本?
- 它们只是作为对鹅的参考来增加这些节点内文本的分数但不保证所有内容都会显示吗?
- goose是否忽略其他常见的通用标签只考虑这些标签?
- 我看到如果 returns 为真,它会跳过添加兄弟姐妹,那是什么意思?
但经过一些调试,我发现提到的标签内的文本没有得到任何特殊的偏好,事实上,不调用已知的文章代码有完全相同的输出以及使用时某些来源的图像提取失败由于某种原因已知的标签。
进一步挖掘后,我还看到函数
def get_known_article_tags(self):
for item in KNOWN_ARTICLE_CONTENT_TAGS:
nodes = self.parser.getElementsByTag(
self.article.doc,
**item)
if len(nodes):
return nodes[0]
return None
在似乎没有任何标签的 article.doc
对象上操作。
在几乎所有帖子中也是如此 returns 只有带有文章标签的元素而不是带有属性的元素 itemprop = articleBody 即使文章中有它们。
调试后 is_articlebody
函数如下代码所示
def is_articlebody(self, node):
for item in KNOWN_ARTICLE_CONTENT_TAGS:
# attribute
if "attr" in item and "value" in item:
if(self.config.debug):
print 'for attr and value'
print self.parser.getAttribute(node, item['attr'])
print item['value']
print node
if self.parser.getAttribute(node, item['attr']) == item['value']:
if(self.config.debug):
print 'is article body from attribute'
return True
# tag
if "tag" in item:
print 'if tag'
print node.tag
if node.tag == item['tag']:
if(self.config.debug):
print 'is article body from tag'
return True
我注意到,即使目标提取文档中有 tags/class 类似的内容,此函数也不会返回 true。
print self.parser.getAttribute(node, item['attr'])
行始终返回为 null 。
如何让鹅获取已知列表attributes/classes/tags中提到的所有文本,就像上面的例子一样,我想获取多个里面的所有文本p 标签(也可以是 p 以外的其他标签)不管分数 ?
编辑:
在尝试进一步调试时,我意识到 get_known_articles_tags 函数只返回在字典中找到的第一个 tag/attribute,
关注:return nodes[0]
所以它只返回文档的那个单个节点,然后它只发送那个节点对象来找到顶级节点——假设该节点不满足 good/top 节点的条件,那么它 returns 为空,因此失败。
如何组合 nodes
列表中的所有节点对象,并将所有节点作为文档发送以解析并使用它来查找顶级节点?
我设法解决了与此问题相关的问题,
我更改了 return 语句的范围并传递了整个数组
def get_known_article_tags(self):
for item in KNOWN_ARTICLE_CONTENT_TAGS:
nodes = self.parser.getElementsByTag(
self.article.doc,
**item)
if len(nodes):
return nodes
return None
然后我将相同的节点数组一次传递给清洁器一个节点(在数组内)并将整个数组传递给 calculate_top_node
函数作为
self.article.top_node = self.extractor.calculate_best_node(doc)
然后在 nodes_to_check
函数中添加了一个额外的循环来检查数组的所有节点,
def nodes_to_check(self, docs):
"""\
returns a list of nodes we want to search
on like paragraphs and tables
"""
nodes_to_check = []
for doc in docs:
for tag in ['p', 'pre', 'td']:
items = self.parser.getElementsByTag(doc, tag=tag)
nodes_to_check += items
return nodes_to_check
这解决了 return 只有单个元素的问题。
我是通过查看 python 3 goose 代码逻辑得出这个结论的,该逻辑在 python2.7 语法上得到了更多的维护和实现。