鱼C论坛

 找回密码
 立即注册
查看: 902|回复: 1

txt中/uxxxx转化为中文问题

[复制链接]
发表于 2019-7-13 14:35:44 | 显示全部楼层 |阅读模式

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

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

x
爬虫过程中在idle中可以输出中文,但是在txt中输出的是如图所示的,要如何改代码才能使txt中存的也是中文
爬取的网页链接http://coral.qq.com/article/2701618945/comment/v2?callback=_article2701618945commentv2&orinum=10&oriorder=o&pageflag=1&cursor=6411378809896418006&scorecursor=0&orirepnum=2&reporder=o&reppageflag=1&source=1&_=1562993284849


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

使用道具 举报

 楼主| 发表于 2019-7-13 14:37:03 | 显示全部楼层
# -*- coding: utf-8 -*-

import urllib.request
import re
import urllib.error
import sys
import emoji

headers = ("User-Agent","Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0")
openr = urllib.request.build_opener()
urllib.request.install_opener(openr)
#url="https://video.coral.qq.com/varticle/2904111363/comment/v2?callback=_varticle2904111363commentv2&orinum=10&oriorder=o&pageflag=1&cursor=6455103461504421579"
url="http://coral.qq.com/article/2701618945/comment/v2?callback=_article2701618945commentv2&orinum=10&oriorder=o&pageflag=1&cursor=6411378809896418006"
for i in range(0,5):
    data = urllib.request.urlopen(url).read().decode()
    #emoji.demojize(str)
    #highpoints = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
    #nickname = highpoints.sub(u'',data)
    patcom = '"content":"(.*?)",'
    patcom1 = '"last":"(.*?)",'
    comdata1 = re.compile(patcom1).findall(data)
    print(type(comdata1[0]))
    comdata = re.compile(patcom).findall(data)
    for j in range(0, len(comdata)):
        print(eval('u"'+comdata[j]+'"'))
        with open('海王.txt','a',encoding='utf8') as file:
            file.write(comdata[j]+'\n')#这样才能转编码格式
            #file.write(comdata[j].encode('utf-8').decode('unicode-escape')+'\n')#这样才能转编码格式
    url="http://coral.qq.com/article/2701618945/comment/v2?callback=_article2701618945commentv2&orinum=10&oriorder=o&pageflag=1&cursor="+comdata1[0]

这是我的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-16 21:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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