⑴ python编写爬虫爬到的中文字符总是乱码,r.encoding也不行
这个页面是gb2312编码的,不是utf-8
⑵ python爬虫抓下来的网页,中间的中文乱码怎么解决
对于python的中文编码问题可以参考下面的帖子
http://python.jobbole.com/85482/
同时,对于网页的中文乱码,建立使用requests模块代替urllib\urllib2
requests的content方法,对中文编码,支持比较好,基本不会出现乱码。
req=requests.get(url,cookies=mecookies)
print req.content
具体用法,参见下面两个帖子,较详细:
http://blog.csdn.net/iloveyin/article/details/21444613
http://blog.csdn.net/alpha5/article/details/24964009
⑶ 求Python大佬解决爬虫乱码
一般乱码问题有可能是以下几种原因导致:
1、编解码方式不对(GKB、UTF8等等,或是干脆用到的编码不支持爬到的文字内容)
2、加解密问题,现在很多平台都有加解密的机制,没有正确方式解密的话,也会出现乱码
3、其他问题,建议具体问题具体分析
可以根据实际的情况具体分析~
⑷ python爬虫抓下来的网页,中间的中文乱码怎么解决
这个肯定是编码的问题,你抓下来的内容要解一下码,你先看下网的的编码,按对应的编码进行解码就可以得到想要的内容了。
比如:read().decode('utf-8')
⑸ 我在写一个python的网络爬虫,写入记事本的内容都是乱码如何使写入的数据以utf8或者gb2312的码制写入。
我从自己一个utf8的爬虫程序里面摘的。
程序开头:
#!/usr/bin/envpython
#-*-coding:utf8-*-
importurllib
importurllib2
importstring
importre
importsys
type0=sys.getfilesystemencoding()#解决中文乱码问题
后面做抓取程序的时候全部加上decode和encode。
pos1=text.find(term.decode("utf-8").encode(type0))
在输入到txt的时候相应的分隔符也要decode和encode:
f.write(info+'!'.decode("utf-8").encode(type0))
希望能帮到你。
⑹ python爬虫爬到的中文乱码怎么办
爬到的内容,肯定是某种编码格式(utf-8/gb2312等)的字符串。只需要对它相应的decode一下就可以了。
比如:如果网页内容是utf-8编码的,就:'xxx'.decode('utf-8');
如果是gb2312编码的,就:'xxx'.decode('gb2312')
⑺ 为什么Python写的爬虫有时候抓取的数据是乱码
# -*- coding:utf-8 -*-
import urllib2
import re
url='http://tieba..com/p/3295185529?see_lz=1'
#打开页面并进行转码
page=urllib2.urlopen(url).read().decode('gbk')
print 'Open %s'%url
#去掉超链接和图片
none_re=re.compile('<a href=.*?>|</a>|<img.*?>')
#换行符转换
br_re=re.compile('<br>')
#标题
title_re=re.compile('<h1 class="core_title_txt " title="(.*?)"')
#帖子内容
content_re=re.compile('<div id="post_content_\d*" class="d_post_content j_d_post_content ">(.*?)</div>')
#搜索文章标题,并去掉文件标题可能含有的特殊符号
title=re.search(title_re,page)
title=title.group(1).replace('\\','').replace('/','').replace(':','').replace('*','').replace('?','').replace('"','').replace('>','').replace('<','').replace('|','')
#搜索文本内容
content=re.findall(content_re,page)
with open('%s.txt'%title,'w') as f:
print 'Writing %s.txt now...'%title
for i in content:
#对html特殊符号进行替换处理
i=re.sub(none_re, '', i)
i=re.sub(br_re, '\n', i)
#写入文本文件
f.write(i.encode('utf-8').strip()+'\n')
print 'Done!'
⑻ 为什么Python写的爬虫有时候抓取的数据是乱码
看下面即可
⑼ 为什么python写的爬虫有时候抓取的数据是乱码
1. 使用chrome浏览器,打开示例页面http://tieba..com/p/3295185529?see_lz=1
2. 在帖子标题处,右键选择"审查元素",可以看到标题的源代码
3. 进行简单的分析,我们需要获取的是title后面的内容,根据页面实际内容,我们编写相应的正则表达式:
title_re=re.compile('<h1 class="core_title_txt " title="(.*?)"')
4. 同理,我们对帖子内容进行"审查元素",得到内容的源代码
5. 编写相应的正则表达式如下:
content_re=re.compile('<div id="post_content_\d*" class="d_post_content j_d_post_content ">(.*?)</div>')
6. 这样通过urllib2打开页面后,使用上述的正则表达式进行匹配,再对标题和文本内容进行相应的处理即可