AbuTang 2016-04-29 02:56 采纳率: 0%
浏览 1719

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条回答 默认 最新

  • 会编程的大白熊 2016-05-07 06:04
    关注
    1. 首先将文件设置为utfu0
    2. print是否打印出信息取决于终端编码,可以将print需要打印的信息先转换为unicode编码,然后再转换为终端对应的编码
    评论

报告相同问题?