首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >:file.write(str)方法get TypeError: write()参数必须是str,而不是Beautifulsoup

:file.write(str)方法get TypeError: write()参数必须是str,而不是Beautifulsoup
EN

Stack Overflow用户
提问于 2016-04-27 10:22:31
回答 1查看 1.4K关注 0票数 1

我写了下面的代码:

代码语言:javascript
复制
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()

哪个编译器出现以下错误:

代码语言:javascript
复制
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,那么为什么编译器会给出第一个错误呢?也不知道为什么第二个会出现。

如果我将代码更改为:

代码语言:javascript
复制
baseUrl = "https://ecurep.mainz.de.xxx.com/ae5/"
#baseUrl = "http://www.bing.com"
#baseUrl = "http://www.sohu.com/"

它将编译并且没有错误。(我已将原名改为"xxx")。

有人能帮我调试一下吗?

更新:

我曾经编写Java代码,是Python的新手。在您的帮助下,我认为我在调试Python方面取得了一些进展:在调试Java时,总是尝试解决第一个错误;而在Python中,则尝试解决最后一个错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-27 11:52:31

尝试用utf-8打开您的文件

代码语言:javascript
复制
import codecs
f = codecs.open("test", "w", "utf-8")

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

代码语言:javascript
复制
f = codecs.open("test", "w", "utf-8", errors='ignore')
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36887065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档