鱼C论坛

 找回密码
 立即注册
查看: 3042|回复: 8

[已解决]html页面的代码保存,怎么才能正常地保存呢

[复制链接]
发表于 2016-8-20 11:20:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
url = "https://cbsprodcenter.noahwm.com/productcenter/pcIndex.jsp"
rget =  requests.get( url , params = query_string_parameters )
txt = rget.text
with open("d:\\html_code.txt","w") as f:
    f.write( str(txt) )
print(txt)
输出结果:UnicodeEncodeError: 'gbk' codec can't encode character '\xa9' in position 7793: illegal multibyte sequence


url = "https://cbsprodcenter.noahwm.com/productcenter/pcIndex.jsp"
rget =  requests.get( url , params = query_string_parameters )
txt = rget.text.encode('utf-8')
with open("d:\\html_code.txt","w") as f:
    f.write( str(txt) )
print(txt)
最佳答案
2016-8-20 21:39:14
import requests
url = "https://cbsprodcenter.noahwm.com/productcenter/pcIndex.jsp"
rget =  requests.get( url)
txt = rget.content#这里用字节集
with open("d:\\html_code.txt","wb") as f:
    f.write(txt)
#print(txt)

输出结果

输出结果
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-20 11:28:35 | 显示全部楼层
rget.encoding = 'utf-8'
txt = rget.text
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-20 11:32:49 | 显示全部楼层
爬虫,分析网页字符集和字符编码的基本技巧。
http://bbs.fishc.com/thread-74916-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-20 12:10:25 | 显示全部楼层
SixPy 发表于 2016-8-20 11:28
rget.encoding = 'utf-8'
txt = rget.text

输出结果还是这个:UnicodeEncodeError: 'gbk' codec can't encode character '\xa9' in position 7793: illegal multibyte sequence
网页的response headers是这样的,貌似就是utf-8的:
Content-Type:text/html;charset=UTF-8
Date:Sat, 20 Aug 2016 04:09:14 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~这咋回事。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-20 12:58:25 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-20 21:39:14 | 显示全部楼层    本楼为最佳答案   
import requests
url = "https://cbsprodcenter.noahwm.com/productcenter/pcIndex.jsp"
rget =  requests.get( url)
txt = rget.content#这里用字节集
with open("d:\\html_code.txt","wb") as f:
    f.write(txt)
#print(txt)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-21 13:41:02 | 显示全部楼层
流氓大叔 发表于 2016-8-20 21:39
import requests
url = "https://cbsprodcenter.noahwm.com/productcenter/pcIndex.jsp"
rget =  request ...

完美
原理是啥,可以分享一下吗~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-21 17:04:53 | 显示全部楼层
jfjjx 发表于 2016-8-21 13:41
完美
原理是啥,可以分享一下吗~~

content返回的是二进制的数据,wb写出的也是二进制的数据,二进制的字节集不需要转码,直接写出。然后你打开的时候再由打开的编辑器进行转码。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-21 22:12:56 | 显示全部楼层
流氓大叔 发表于 2016-8-21 17:04
content返回的是二进制的数据,wb写出的也是二进制的数据,二进制的字节集不需要转码,直接写出。然后你 ...

学习了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-22 07:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表