我寫了下面的代碼:的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試圖解決最後一個問題。對?
你得到一個'UnicodeEncodeError',而不是'TypeError'你自稱得到。 –
正如@MartijnPieters所說,你有一個編碼錯誤,可能是因爲你要加載的網頁包含一些特殊字符。 – gdlmx