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'
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()
You should remove the sys.setdefaultencoding
. Notice that this has been an abuse of sys.setdefaultencoding
all along in Python 2 too. From Python 2 documentation:
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
site
module implementation and, where needed, bysitecustomize
. Once used by thesite
module, it is removed from thesys
module’s namespace.New in version 2.0.
This set the encoding for 8-bit strings in Python 2. Since bytestrings have no encoding 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 nothing to set the default encoding for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With