a447685024
AbuTang
2016-04-29 02:56

python 爬虫 出现IOError错误

-*- coding:utf-8 -*-

#! /usr/bin/env python
import urllib
import urllib2
import re

page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'Mozilla/4.0(compatible;MSIE 5.5;Windows NT)'
headers = {'User-Agent':user_agent}

try:
request = urllib2.Request(url,headers = headers)
response = urllib2.urlopen(request)
content = response.read().decode('utf-8','ignore')
pattern = re.compile(r'

?<img.?>.*?.*?.*?(.*?)', re.S)
items = re.findall(pattern,content)
for item in items:
print item[0],item[1]

except urllib2.URLError, e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason


```这是出现错误的信息,在powershell中设置chcp 65001就会出现IOError错误,设置成chcp 437或者936就会出现下面的乱码,请问是什么原因?


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐