UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position

zzldh9 2018-02-09 11:18:51
爬虫代码如下:
import requests,json
import pymysql
#1.请求一个单页的内容
def get_one_page(url):
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/58.0'}
response=requests.get(url,headers=headers)
return response.content.decode('utf-8')
#解析、
data = []
def parse_one_page(html):
dataJson=json.loads(html)
print(html)
# print(dataJson['data']);
for n in dataJson['data']['data']:
c=n['summary']
yield c
#3.保存(将数据存入数据库)
def write_to_mysql(content):
conn=pymysql.connect(host='localhost',user='root',passwd='123456',db='neihan',charset='utf8')
cur=conn.cursor()
neirong=content
sql='insert into duanzi values(NULL,%s)'
parm=(neirong,)
try:
cur.execute(sql,parm)
conn.commit()
except Exception:
pass
cur.close()
conn.close()
#4.定义主函数
def main(timestamp):
# while type(timestamp)==int or type(timestamp)==float:
for j in range(1):
url='http://pclistinterface.mop.com/mdi/data.html?jsonpcallback=' \
'success_jsonpCallback&pgnum=1&colid=110035&pgsize=20&serialnum' \
'=300000&startcol=10019&mirrorid=1518140521&platform=pc&uid=' \
'15179020020634794&qid=null&login=null&_='+str(timestamp)
print(url)
html=get_one_page(url)
# dataJson = json.loads(html)
i=1
for item in parse_one_page(html):
#print(item)
item=item.strip()
if item:
write_to_mysql(item.strip())
i+=1
#imestamp=dataJson['data']['max_time']
main(1518140521)
...全文
1354 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2018-02-09
  • 打赏
  • 举报
回复 1
把对应出错行数的字符等用unicode解码编码处理一下

37,718

社区成员

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

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