2016-04-27 53 views
0

我写了下面的代码:的Python Beautifulsoup:file.write(STR)方法来获取类型错误:写()参数必须海峡,不BeautifulSoup

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() 

该编译器在下面的错误:(但soup_string显然是一个?海峡,为什么由编译器 也给出了第一个错误不知道为什么第二个出现)

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> 

更重要的是混乱,如果我改变代码:

baseUrl = "https://ecurep.mainz.de.xxx.com/ae5/" 
#baseUrl = "http://www.bing.com" 
#baseUrl = "http://www.sohu.com/" 

它会编译并且没有错误。 (我把原来的名字改为“xxx”)。

任何人都可以帮助调试吗?

-----更新:

我以前写Java代码或我为Python新手。在你的帮助下,我想我已经在调试Python方面取得了一些进展:在调试Java时,总是尝试解决第一个错误;而在Python中,ayways试图解决最后一个问题。对?

+0

你得到一个'UnicodeEncodeError',而不是'TypeError'你自称得到。 –

+0

正如@MartijnPieters所说,你有一个编码错误,可能是因为你要加载的网页包含一些特殊字符。 – gdlmx

回答

2

尝试打开你的文件,utf-8

import codecs 
f = codecs.open("test", "w", "utf-8") 

可以忽略编码错误(不推荐)由

f = codecs.open("test", "w", "utf-8", errors='ignore') 
+0

是的,它确实有效。关于调试的一些东西:我曾经写Java代码,并且是Python的新手。在你的帮助下,我想我已经在调试Python方面取得了一些进展:在调试Java时,总是尝试解决第一个错误;而在Python中,ayways试图解决最后一个问题。对? – ZhaoGang

+0

也许你没有在你的回溯中没有注意到这条消息:_“(最近的最后一次通话)”_ – gdlmx

相关问题