Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Beautifulsoup beautifulsou>prettify()方法仅在一行中显示整个输出_Beautifulsoup_Python 3.4 - Fatal编程技术网

Beautifulsoup beautifulsou>prettify()方法仅在一行中显示整个输出

Beautifulsoup beautifulsou>prettify()方法仅在一行中显示整个输出,beautifulsoup,python-3.4,Beautifulsoup,Python 3.4,这是我在stackoverflow.com上的第一篇文章,我有一个问题,就是pydevforeclipse控制台中显示的Python3程序的输出 我正在使用: Python 3.4-- 用于Eclipse的PyDev- Python模块:请求、bs4、pprint 每当我运行下面的代码时 html_content = response.content bs = BS(html_content,'html.parser') page_html = bs.prettify(encoding='utf

这是我在stackoverflow.com上的第一篇文章,我有一个问题,就是pydevforeclipse控制台中显示的Python3程序的输出

我正在使用: Python 3.4-- 用于Eclipse的PyDev- Python模块:请求、bs4、pprint

每当我运行下面的代码时

html_content = response.content
bs = BS(html_content,'html.parser')
page_html = bs.prettify(encoding='utf-8')
print(page_html)
整个输出单独显示在一行中,如下所示,而不是以漂亮的打印格式显示

b'<!DOCTYPE doctype html>\n<html class="no-js" lang="en-US">\n <head>\n  <meta charset="utf-8"/> ...<entire output>...
我还尝试了pprint模块中的pprint方法。但是,我得到了相同的结果,即整个输出仅显示在一行中

如何使o/p以preety打印格式显示

谢谢, skambl

指定编码参数时,要求它对输出进行编码。这将为您提供一个bytes对象,打印字符串之前的前导b可以识别该对象。打印到控制台的“some value”表示您在Python3中打印了一个bytes对象

选择1

因为您要求将其编码为utf-8,所以您应该将其解码为utf-8

选择2

看起来您实际上想要的是字符串而不是字节对象。那就这么做吧

page_html = bs.prettify()  # no encoding parameter
此外,您可能需要阅读上的部分,以了解可以对输出执行的更多操作

我知道你很久以前问过这个问题,但希望答案仍然特别有用,因为知道前导的b'…'是一个字节对象,你需要解码它!我在寻找与bs4相关的东西,偶然发现了这个,我想我可以解释一下你为什么会看到这种行为:

page_html = bs.prettify()  # no encoding parameter