python爬出来的中文怎么是乱码啊?

港湾2019 2017-08-10 03:32:03

import requests
from bs4 import BeautifulSoup
import sys
import urllib2

response = requests.get('http://www.163.com')
content = response.content
soup = BeautifulSoup(content,'lxml')
ul_list = soup.find_all('ul',attrs={'class':'cm_ul_round_r'})
f1 = open('/cs/cc.txt','w')
f1.write(ul_list)
f1.close()

为什么写不进去啊?
...全文
155 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
港湾2019 2017-08-12
  • 打赏
  • 举报
回复
谢谢谢谢 受教了~!
宇风 -飞扬 2017-08-10
  • 打赏
  • 举报
回复
知其然,知其所以然,仔细分析报错原因, 不要一有错误,就想着找别人给你解决,这不是一个优秀程序员,该有的思维!
宇风 -飞扬 2017-08-10
  • 打赏
  • 举报
回复


这里写错了
 ul_list = soup.find_all('ul',attrs={'class':'cm_ul_round_r'}),

返回的ul_list 是一个列表,所应该改为f1.write(str(ul_list[0]))

ul_list 是一个列表,
ul_list[0] 取到列表元素的值,这个并不是一个str
f1.write() 中必须是str 所以f1.write(str(ul_list[0]))

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧