本文整理匯總了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