|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 gps_davids 于 2018-12-25 14:58 编辑
求助:
这道题我在最后导入的过程中使用了pickle.dump(), 为何报错了呀?
报错的提示是:
File "<ipython-input-10-bee527f5ec53>", line 21, in main
pickle.dump(html, each_file)
TypeError: write() argument must be str, not bytes
我看到正确答案中使用的是write方法。
源代码如下:
- import urllib.request
- import chardet
- import pickle
- def main():
-
- with open("053urls.txt", "r") as f:
- urls = f.read().splitlines()
- i = 0
- for each_url in urls:
- i += 1
- response = urllib.request.urlopen(each_url)
- html = response.read()
-
- encode = chardet.detect(html)['encoding']
- if encode == 'GB2312':
- encode = 'GBK'
- html = html.decode(encode, "ignore") #为何要ignore?
-
- with open('053url_' + str(i) + '.txt', 'w', encoding=encode) as each_file: #注意格式
- pickle.dump(html, each_file)
- main()
复制代码
|
|