php抓取京东乱码,python,beautifulsoup_爬取京东页面的文本为乱码,python,beautifulsoup - phpStudy...

爬取京东页面的文本为乱码

我使用beautiful soup解析京东的界面,把里面的文本全都提取出来,但是打印的时候发现全是乱码。jd的界面使用utf-8编码的,我在解码成gbk时却遇到错误。

下面是代码,请指教。

#encoding=gbk

from bs4 import BeautifulSoup

from bs4 import NavigableString

from bs4 import Comment

from bs4 import Doctype

import urllib2

def walker(soup, indent):

text=""

if soup.name is not None:

for child in soup.children:

if isinstance(child, NavigableString):

if len(child) != 1: #如何判断是否为空

text = indent + unicode(child).encode('utf-8').strip() #.decode('utf-8').encode('gbk')

text += walker(child, indent+"\t")

return text

if __name__ == "__main__":

soup = BeautifulSoup( urllib2.urlopen("http://item.jd.com/1592573020.html").read())

doctypes=soup.findAll(text=lambda text: isinstance(text, Doctype))

[doctype.extract() for doctype in doctypes]

comments = soup.findAll(text=lambda text:isinstance(text, Comment))

[comment.extract() for comment in comments]

for script in soup("script"):

script.extract()

for noscript in soup("noscript"):

noscript.extract()

for style in soup("style"):

style.extract()

text=walker(soup, "")

print "text", text.decode('utf-8').encode('gbk') #这里会出错

相关阅读:

jquery hover mouseenter mouseleave事件只触发一次动画

AFNetworking-3.x 分支编译不通过

docker在centos7 中安装后启动失败

安卓中,声纹如何显示

在ubuntu上装了node.js后,npm install hexo-cli -g 提示没装npm,怎么办

myeclipse错误提示窗口

css 实现input 获得焦点时 placeholder向上移动并且输入框下边框样式改变

vue中用$index获取一维数组的index值。那二维数组的index值如何获取呢?

微信分享 icon图片不显示问题

如何不自动换行

docker pull ubuntu 总是出错

WPF:判断ListBox中CheckBox控件是否被勾选,并获得所有被勾选控件的Content

用 vim 编辑怎样才能比 IDE 更快

有关Xcode 工程中的设置Architectures

react 如何销毁组件,并触发销毁事件?

js获取 和 ajax发送的问题

红米手机java.io.IOException: open failed: EACCES (Permission denied)

交互式Ruby换行时字符错乱

浏览器调试工具中的分号分隔的cookie怎么转换为字典?

如何将百度地图的坐标经纬度转换成GPS坐标

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值