鱼C论坛

 找回密码
 立即注册
查看: 2862|回复: 2

爬取新浪微博图片关于UnicodeDecodeError报错的问题

[复制链接]
发表于 2016-4-8 18:01:16 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 三林小猎人 于 2016-4-8 18:11 编辑
  1. import urllib.request
  2. import re


  3. def open_url(url):
  4.   req = urllib.request.Request(url)
  5.   req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.11466.7 Safari/537.36')
  6.   
  7.   page = urllib.request.urlopen(req)
  8.   html = page.read().decode('utf-8')

  9.   return html

  10. def get_img(html):
  11.   p = r'<img node-type="imgShow" src="(^http+\.jpg)"'
  12.   imglist = re.findall(p,html)


  13.   for each in imglist:
  14.     filename = each.split('/')[-1]
  15.     urllib.request.urlretrieve(each,filename,None)
  16.   


  17. if __name__ == '__main__':
  18.   url = 'http://weibo.com/u/3640184915/home?wvr=5'
  19.   get_img(open_url(url))
复制代码


Traceback (most recent call last):
  File "C:\Users\dell\Desktop\weibotupian.py", line 27, in <module>
    get_img(open_url(url))
  File "C:\Users\dell\Desktop\weibotupian.py", line 10, in open_url
    html = page.read().decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 319: invalid continuation byte

查看网页确实是utf-8编码格式呀!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-8 20:02:59 | 显示全部楼层
试下decode('utf-8', 'ignore')

有时候网页里有些奇怪的字符
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-8 20:25:03 | 显示全部楼层
blurrye 发表于 2016-4-8 20:02
试下decode('utf-8', 'ignore')

有时候网页里有些奇怪的字符

好的,谢谢啦!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-20 14:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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