码迷,mamicode.com
首页 > 编程语言 > 详细

Python 出现 can't use a string pattern on a bytes-like object

时间:2017-02-26 17:49:00      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:like   com   highlight   art   data   span   key   can   request   

  1. # coding=utf-8
  2. import urllib.request
  3. import re
  4. url = ‘http://www.163.com‘
  5. file = ‘d:/test.html‘
  6. data = urllib.request.urlopen(url).read()
  7. r1 = re.compile(‘<.*?>‘)
  8. c_t = r1.findall(data)
  9. print(c_t)

发现读取下来后,运行到第9 行,出现:

can‘t use a string pattern on a bytes-like object

查找了一下,是说3.0现在的参数更改了,现在读取的是bytes-like的,但参数要求是chart-like的,找了一下,加了个编码:

data = data.decode(‘utf-8‘)

 

在与正则使用前,就可以正常使用了..

Python 出现 can't use a string pattern on a bytes-like object

标签:like   com   highlight   art   data   span   key   can   request   

原文地址:http://www.cnblogs.com/andrewleeeeee/p/6444906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!