找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3137|回复: 1

[求助] 基于scrapy的一个简单爬虫,编码老是有问题

2

主题

3

帖子

3

积分

贫民

积分
3
renwo713 发表于 2016-7-25 12:47:45 | 显示全部楼层 |阅读模式
我的spider
  1. # -*- coding: utf-8 -*-
  2. from scrapy.spiders import Spider
  3. from scrapy.selector import Selector  
  4.   
  5. from scrapy.http import Request
  6. from Myspider.items import MyspiderItem


  7. class BaiduSpider(Spider):
  8.     name = "w3school"
  9.     allowed_domains = ["263zw.com"]
  10.     start_urls =  [  
  11.         "http://www.263zw.com/53103/7518437.html"  
  12.     ]  
  13.    
  14.     def parse(self, response):
  15.         sel = Selector(response)
  16.         items = []
  17.         maindiv=sel.xpath('//div[@class="main"]');
  18.         print maindiv.extract()
  19.         
  20.         title=maindiv[0].xpath('div/h3/text()').extract()
  21.         desc=maindiv[0].xpath('//div[@id="chapterContent"]/text()').extract()
  22.         encdesc=desc[0].decode('gbk','ignore')
  23.         enctitle=title[0]
  24.         print u'title:'+enctitle
  25.         print u'desc:'+encdesc
  26.         item = MyspiderItem()
  27.         item['title']=enctitle
  28.         item['desc']=encdesc
  29.         
  30.         return item
  31.         
  32.         #items = []  
  33.   
  34.         #log.msg("Append done.",level='INFO')  
  35.         #return items  
  36.         #获得下一篇文章的url
  37.         #urls = sel.xpath('//div[@class="page"]/a/@href').extract()
  38.         #url="http://www.263zw.com" +urls[2]
  39.         
  40.         #print url
  41.         #yield Request(url, callback=self.parse)#再次请求新的URL  
  42.         
复制代码


pipeline是
  1. # -*- coding: utf-8 -*-

  2. # Define your item pipelines here
  3. #
  4. # Don't forget to add your pipeline to the ITEM_PIPELINES setting
  5. # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

  6. import json  
  7. import codecs  

  8. class MyspiderPipeline(object):
  9.    
  10.     def __init__(self):  
  11.         self.file = codecs.open('D://w3school_data_utf8.json', 'wb', encoding='utf-8')
  12.         
  13.         
  14.     def process_item(self, item, spider):
  15.         print 'hello '+item['desc'].encode("GBK","ignore")
  16.             #self.file.write(line['title'].decode("UTF-8", "ignore"))  
  17.         #print line
  18.         self.file.write(item['desc'].encode("unicode_escape"))  
  19.         return item
复制代码


我爬取的网站是一个小说站,http://www.263zw.com/53103/7518437.html   ,  不知道是不是有防爬取的代码,

错误在spider的  :encdesc=desc[0].decode('gbk','ignore')   ,
报错是 UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-57: ordinal not in range(128)
如果我不加.decode('gbk','ignore') , 就会报 gbk什么错误illegal multibyte sequence python, 也是编码的问题,
不知道该怎么办了,  有知道的大神吗, 求救啊

回复

使用道具 举报

50

主题

1057

帖子

1108

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1108

活跃会员热心会员最佳导师默默耕耘突出贡献优秀版主荣誉管理论坛元老

blueelwang 发表于 2016-7-25 16:46:09 | 显示全部楼层
print 'hello '+item['desc'].encode("GBK","ignore")

这一行出错了吧
encode的作用是将unicode编码转换成其他编码的字符串

建议看一下这篇文章: http://www.pythontab.com/html/2016/pythonjichu_0713/1043.html
回复 支持 反对

使用道具 举报

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

本版积分规则

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