本文整理汇总了Python中bs4.element.Comment方法的典型用法代码示例。如果您正苦于以下问题:Python element.Comment方法的具体用法?Python element.Comment怎么用?Python element.Comment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bs4.element
的用法示例。
在下文中一共展示了element.Comment方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: text_only
# 需要导入模块: from bs4 import element [as 别名]
# 或者: from bs4.element import Comment [as 别名]
def text_only(self):
"""Returns the displayed text of a webpage (without HTML tags)"""
if "text" in self.type and self.size:
texts = self.soup.findAll(text=True)
def is_visible(element):
if len(element.strip()) == 0:
return False
elif isinstance(element, Comment):
return False
elif element.parent.name in ["style", "script", "[document]", "head", "title"]:
return False
return True
text = " ".join(filter(is_visible, texts)).replace("\r\n", " ").replace("\n", " ")
return text
return ""
示例2: comment
# 需要导入模块: from bs4 import element [as 别名]
# 或者: from bs4.element import Comment [as 别名]
def comment(self, content):
"Handle comments as Comment objects."
self.soup.endData()
self.soup.handle_data(content)
self.soup.endData(Comment)
示例3: tag_visible
# 需要导入模块: from bs4 import element [as 别名]
# 或者: from bs4.element import Comment [as 别名]
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
示例4: study
# 需要导入模块: from bs4 import element [as 别名]
# 或者: from bs4.element import Comment [as 别名]
def study(bs_node, parent=None, keyword=""):
entries = []
# if parent is None:
# print("Keyword is: {0}".format(keyword))
if keyword in str(bs_node).lower():
if isinstance(bs_node, element.Tag):
if keyword in str(bs_node.attrs):
for k, v in bs_node.attrs.items():
if keyword in v:
# print("Found in attribute value {0} of tag {1}".format(k, bs_node.name))
noscript = close_noscript(bs_node)
d = {"type": "attrval", "name": k, "tag": bs_node.name, "noscript": noscript}
if d not in entries:
entries.append(d)
if keyword in k:
# print("Found in attribute name {0} of tag {1}".format(k, bs_node.name))
noscript = close_noscript(bs_node)
d = {"type": "attrname", "name": k, "tag": bs_node.name, "noscript": noscript}
if d not in entries:
entries.append(d)
elif keyword in bs_node.name:
# print("Found in tag name")
noscript = close_noscript(bs_node)
d = {"type": "tag", "value": bs_node.name, "noscript": noscript}
if d not in entries:
entries.append(d)
# recursively search injection points for the same variable
for x in bs_node.contents:
for entry in study(x, parent=bs_node, keyword=keyword):
if entry not in entries:
entries.append(entry)
elif isinstance(bs_node, element.Comment):
# print("Found in comment, tag {0}".format(parent.name))
noscript = close_noscript(bs_node)
d = {"type": "comment", "parent": parent.name, "noscript": noscript}
if d not in entries:
entries.append(d)
elif isinstance(bs_node, element.NavigableString):
# print("Found in text, tag {0}".format(parent.name))
noscript = close_noscript(bs_node)
d = {"type": "text", "parent": parent.name, "noscript": noscript}
if d not in entries:
entries.append(d)
return entries
# generate a list of payloads based on where in the webpage the js-code will be injected