我写了下面的代码:
from bs4 import BeautifulSoup
import sys # where is the sys module in the source code folder ?
try:
import urllib.request as urllib2
except ImportError:
import urllib2
print (sys.argv) #
print(type(sys.argv)) #
#baseUrl = "https://ecurep.mainz.de.xxx.com/ae5/"
baseUrl = "http://www.bing.com"
baseUrl = "http://www.sohu.com/"
print(baseUrl)
url = baseUrl
page = urllib2.urlopen(url) #urlopen is a function, function is also an object
soup = BeautifulSoup(page.read(), "html.parser") #NameError: name 'BeautifulSoup' is not defined
html_file = open("Output.html", "w")
soup_string = str(soup)
print(type(soup_string))
html_file.write(soup_string) # TypeError: write() argument must be str, not BeautifulSoup
html_file.close()哪个编译器出现以下错误:
C:\hzg>py Py_logDownload2.py 1
['Py_logDownload2.py', '1']
<class 'list'>
http://www.sohu.com/
<class 'str'>
Traceback (most recent call last):
File "Py_logDownload2.py", line 25, in <module>
html_file.write(soup_string) # TypeError: write() argument must be str, not
BeautifulSoup
File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python35\lib\encodings\
cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 376-377:
character maps to <undefined>但是soup_string显然是一个str,那么为什么编译器会给出第一个错误呢?也不知道为什么第二个会出现。
如果我将代码更改为:
baseUrl = "https://ecurep.mainz.de.xxx.com/ae5/"
#baseUrl = "http://www.bing.com"
#baseUrl = "http://www.sohu.com/"它将编译并且没有错误。(我已将原名改为"xxx")。
有人能帮我调试一下吗?
更新:
我曾经编写Java代码,是Python的新手。在您的帮助下,我认为我在调试Python方面取得了一些进展:在调试Java时,总是尝试解决第一个错误;而在Python中,则尝试解决最后一个错误。
发布于 2016-04-27 11:52:31
尝试用utf-8打开您的文件
import codecs
f = codecs.open("test", "w", "utf-8")您可以忽略编码错误(不推荐)
f = codecs.open("test", "w", "utf-8", errors='ignore')https://stackoverflow.com/questions/36887065
复制相似问题