本文整理汇总了Python中six.moves.html_parser.HTMLParser.encode方法的典型用法代码示例。如果您正苦于以下问题:Python HTMLParser.encode方法的具体用法?Python HTMLParser.encode怎么用?Python HTMLParser.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.html_parser.HTMLParser
的用法示例。
在下文中一共展示了HTMLParser.encode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: display_link_prompt
# 需要导入模块: from six.moves.html_parser import HTMLParser [as 别名]
# 或者: from six.moves.html_parser.HTMLParser import encode [as 别名]
def display_link_prompt(args, urls, titles):
"""Print URLs and their descriptions alongside a prompt.
Keyword arguments:
args -- program arguments (dict)
urls -- search URLs found (list)
titles -- descriptions of search URLs found (list)
"""
while 1:
print('\n{0}'.format(BORDER))
for i in range(len(urls)):
link = HTMLParser().unescape(titles[i])
print('{0}. {1}'.format(i+1, link.encode('utf-8') if PY2 else link))
print(BORDER)
# Handle link prompt input
try:
link_input = [inp.strip() for inp in input(': ').split()]
if not link_input:
continue
utils.check_input(link_input) # Check input in case of quit
print('\n')
exec_prompt_cmd(args, urls, link_input[0], link_input[1:])
except (KeyboardInterrupt, EOFError, ValueError, IndexError):
return False
示例2: save
# 需要导入模块: from six.moves.html_parser import HTMLParser [as 别名]
# 或者: from six.moves.html_parser.HTMLParser import encode [as 别名]
def save(self, page, crawl=False):
"""Requests and saves a remote page to a local file
"""
print('Saving '+page)
html = self.fetch(page, crawl)
content = html.replace('</p>', '\n')
content = re.sub(r'<.*?>', ' ', content)
content = HTMLParser().unescape(content)
content = content.encode('utf8')
if self.out_file:
with open(self.out_file, 'a') as handle:
handle.write(content+'\n')
elif self.out_dir:
page_key = self.page_key(page)
try:
os.makedirs(os.path.join(self.out_dir, page_key[0]))
except OSError:
pass
filename = os.path.join(self.out_dir, page_key[0],
(page_key[1]+'?'+page_key[2]).replace('/', '_'))
with open(filename, 'w') as handle:
handle.write(content)