新人求助贴
python3吧
全部回复
仅看楼主
level 1
代码:
import requests
res = requests.get('https://tieba.baidu.com/p/8527814666')
res.raise_for_status()
playFile = open('D:\\123456.html', 'w')
for chunk in res.iter_content(100000):
playFile.write(chunk)
playFile.close()
问题:
playFile.write(chunk)
TypeError: write() argument must be str, not bytes
目的:爬取该网站网页源码
python3.8.5的版本win10系统
有什么好的解决方案吗
2023年08月05日 06点08分 1
level 8
(本人也是新人,回答未必准确)
看你的报错应该是chunk是字节类型的,而你打开文件用的是‘w’,只能写入字符串类型的数据,所以报错了。
大概有两个解决思路
1.playFile = open('D:\\123456.html', 'w')改成playFile = open('D:\\123456.html', 'wb')
2.playFile.write(chunk)改成playFile.write(chunk.decode('utf-8'))
二选其一就可以了(大概?)
希望我的回答对你有帮助,如果说错了那么非常抱歉
2023年08月06日 11点08分 3
level 8
刚刚回的帖突然就被删了?![疑问]
2023年08月06日 11点08分 4
1