Python AttributeError: 模块“sys”没有属性“setdefaultencoding”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45252305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:51:52  来源:igfitidea点击:

AttributeError: module 'sys' has no attribute 'setdefaultencoding'

pythonpython-3.xencodingweb-scraping

提问by yome

My original code is this.

我的原始代码是这样的。

#py3.6, windows10   
import time
from selenium import webdriver
import codecs
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

Reload is not supported. It was fixed.

不支持重新加载。它是固定的。

Import importlib
Importlib.reload (sys)

But there was also an error.

但也出现了错误。

AttributeError: module 'sys' has no attribute 'setdefaultencoding'

AttributeError: 模块“sys”没有属性“setdefaultencoding”

How should I fix this? I would appreciate your help.

我应该如何解决这个问题?我会很感激你的帮助。

I also attach my entire code.

我还附上了我的整个代码。

import time
from selenium import webdriver
import codecs
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

browser = webdriver.PhantomJS('C:\phantomjs-2.1.1-windows/bin/phantomjs')
url = u'https://twitter.com/search?f=tweets&vertical=default&q=%EB%B0%B0%EA%B3%A0%ED%8C%8C%20since%3A2017-07-19%20until%3A2017-07-20&l=ko&src=typd&lang=ko'

browser.get(url)
time.sleep(1)

body = browser.find_element_by_tag_name('body')
browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")

start = time.time()
for _ in range(500):
    now = time.time()
    browser.execute_script("window.scrollTo(0, 
    document.body.scrollHeight);")
    print str(_) + "    seconds: " + str(now - start) 
    time.sleep(0.2)

tweets=browser.find_elements_by_class_name('tweet-text')

with codecs.open("test.txt", "w","utf-8") as f:
    i = 1
    for i, tweet in enumerate(tweets):
        data = tweet.text
        data = data.encode('utf-8')
        print i, ":", data
        msg = (str(data) +'\n')
        f.write(msg)
        i += 1

end = time.time()
print(end - start)
browser.quit()

回答by Antti Haapala

You should remove the sys.setdefaultencoding. Notice that this has been an abuse of sys.setdefaultencodingall along in Python 2 too. From Python 2 documentation:

您应该删除sys.setdefaultencoding. 请注意,这sys.setdefaultencoding在 Python 2 中也一直被滥用。来自 Python 2 文档

sys.setdefaultencoding(name)

Set the current default string encoding used by the Unicode implementation. If name does not match any available encoding, LookupError is raised. This function is only intended to be used by the sitemodule implementation and, where needed, by sitecustomize. Once used by the sitemodule, it is removed from the sysmodule's namespace.

New in version 2.0.

sys.setdefaultencoding(name)

设置 Unicode 实现使用的当前默认字符串编码。如果名称与任何可用编码不匹配,则会引发 LookupError。此函数仅供site模块实现使用,并在需要时由sitecustomize. 一旦被site模块使用,它就会从sys模块的命名空间中删除。

2.0 版中的新功能。

This set the encoding for 8-bit strings in Python 2. Since bytestrings have noencoding in Python 3, and unicode strings (str) have neither (they're Unicode, but with opaque internal encoding), this function would be meaningless on Python 3 - there is nothingto set the default encoding for.

这在 Python 2 中设置了 8 位字符串的编码。由于字节串在 Python 3 中没有编码,而 unicode 字符串 ( str) 两者都没有(它们是 Unicode,但具有不透明的内部编码),这个函数在 Python 3 上将毫无意义- 没有什么可以设置默认编码的。