简体   繁体   English

VSCode中的任务输出编码

[英]Task output encoding in VSCode

I'm learning BeautifullSoup with Visual Studio Code and when I run this script: 我正在使用Visual Studio Code学习BeautifullSoup,并且在运行此脚本时:

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent

ua = UserAgent()
header = {'user-agent':ua.chrome}
google_page = requests.get('https://www.google.com',headers=header)

soup = BeautifulSoup(google_page.content,'lxml') # html.parser

print(soup.prettify())

And I'm getting the following error: 我收到以下错误:

Traceback (most recent call last): File "c:\\ ... \\intro-to-soup-2.py", line 13, in print(soup.prettify()) File "C:\\ ... \\Local\\Programs\\Python\\Python36-32\\lib\\encodings\\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\\U0001f440' in position 515: character maps to 追溯(最近一次通话):文件“ c:\\ ... \\ intro-to-soup-2.py”,行13,在print(soup.prettify())中文件“ C:\\ ... \\ Local \\ Programs \\ Python \\ Python36-32 \\ lib \\ encodings \\ cp1252.py“,第19行,编码返回编解码器。charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError:'charmap'编解码器无法编码字符位置515中的'\\ U0001f440':字符映射到

If I force the encoding for utf-8 in the soup variable I won't be abble to use prettify as it doesn't work with strings... Also tried using # - - coding: utf-8 - - on the first line of code without sucess. 如果我在汤变量中强制对utf-8进行编码,那么我不会迷惑地使用prettify,因为它不适用于字符串...也尝试在第一行使用# --编码:utf- 8--没有成功的代码。

Here is my tasks.json for this project: 这是该项目的我的task.json:

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "python",
"isShellCommand": true,
"args": ["${file}"],
"files.encoding": "utf8",
// Controls after how many characters the editor will wrap to the next line. Setting this to 0 turns on viewport width wrapping (word wrapping). Setting this to -1 forces the editor to never wrap.
"editor.wrappingColumn": 0, // default value is 300
// Controls the font family.
"editor.fontFamily": "Consolas, 'Malgun Gothic', '맑은 고딕','Courier New', monospace",
// Controls the font size.
"editor.fontSize": 15,
"showOutput": "always"
}

The exact same code is running in PyCharm without any problems. 完全相同的代码在PyCharm中运行没有任何问题。 Any ideas how I can fix this in Visual Studio Code? 有什么想法可以在Visual Studio Code中解决吗?

Here's my "pip freeze" result: 这是我的“冻结点数”结果:

astroid==1.5.3
beautifulsoup4==4.5.3
colorama==0.3.9
fake-useragent==0.1.7
html5lib==0.999999999
isort==4.2.15
lazy-object-proxy==1.3.1
lxml==3.7.2
mccabe==0.6.1
pylint==1.7.1
requests==2.12.5
selenium==3.4.3
six==1.10.0
webencodings==0.5
wrapt==1.10.10
xlrd==1.0.0
XlsxWriter==0.9.6

Thank you for your time, 感谢您的时间,

Eunito. Eunito。

The problem here seems to be the encoding the python interpreter believes stdout/stderr support. 这里的问题似乎是python解释器认为stdout / stderr支持的编码。 For some reason (arguably, a bug in VSCode) this is set to some platform-specific value (cp1252 in windows for you, I was able to reproduce the issue on OS X and got ascii) instead of utf-8 which the VSCode output window supports. 由于某种原因(可以说是VSCode中的错误),它被设置为特定于平台的值(Windows中为cp1252,对于您来说,我能够在OS X上重现该问题并得到ascii),而不是utcode-8(VSCode输出)窗口支持。 You can modify your task.json to look something like this to address this - it sets an environment variable forcing the Python interpreter to use utf8 for output. 您可以修改task.json使其看起来像这样来解决此问题-它设置了一个环境变量,强制Python解释器使用utf8进行输出。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "python3",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always",
    "options": {
        "env": {
            "PYTHONIOENCODING":"utf-8"
        }
    }
}

The relevant bit is the "options" dictionary. 相关位是“选项”字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM