[Scrapy]爬取糗事百科段子

1.Python爬虫实战一之爬取糗事百科段子
http://cuiqingcai.com/990.html
2.在工作目录创建myproject

scrapy startproject myproject

3.编写/myproject/myproject/items.py

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html

import scrapy

#Item是需要的数据进行格式化,方便后期处理
class MyItem(scrapy.Item):
    user = scrapy.Field()
    content = scrapy.Field()
    godComment = scrapy.Field()

4.编写/myproject/myproject/spiders/MySpider.py

# -*- coding:utf-8 -*-
import scrapy
import re

from myproject.items import MyItem

#Spider是指定URL,发送请求和接收原始数据,再根据Item进行数据操作
class MySpider(scrapy.Spider):
    name = 'myspider'

    #可传入pageIndex参数合成完整URL
    def __init__(self, pageIndex=None, *args, **kwargs):
        super(MySpider, self).__init__(*args, **kwargs)
        self.start_urls = ['http://www.qiushibaike.com/hot/page/%s' % pageIndex]

    #根据Item进行数据操作
    def parse(self, response):
        #print response.body.decode('response.encoding') #打印原始数据
        pattern = re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>' + 
                        '.*?' + 
                        '<div class="content">.*?<span>(.*?)</span>.*?</div>' +
                         '.*?' + 
                        '<div class="main-text">(.*?)<div class="likenum">'
                        ,re.S)
        items = re.findall(pattern,response.body.decode(response.encoding))
        print ("lin len: %d"%(len(items)))
        for item in items:         
            print ("lin User: %s"%(item[0].strip()))
            print ("lin Content: %s"%(item[1].strip()))
            print ("lin God comments: %s"%(item[2].strip()))
            myItems = MyItem(user=item[0], content=item[1], godComment=item[2])
            yield myItems

5.设置/myproject/myproject/settings.py的headers

DEFAULT_REQUEST_HEADERS = {
  'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
}

6.执行myspider,传入pageIndex=1,结果数据保存为items.json

scrapy crawl myspider -a pageIndex=1 -o items.json

7.结果输出和items.json出现Export Unicode字符集问题
这里写图片描述
8.Scrapy中关于Export Unicode字符集问题解决
http://blog.csdn.net/peihaozhu/article/details/53022236
8.1设置/myproject/myproject/settings.py

from scrapy.exporters import JsonLinesItemExporter  
class CustomJsonLinesItemExporter(JsonLinesItemExporter):  
    def __init__(self, file, **kwargs):  
        super(CustomJsonLinesItemExporter, self).__init__(file, ensure_ascii=False, **kwargs)

#这里只需要将超类的ensure_ascii属性设置为False即可
#同时要在setting文件中启用新的Exporter类

FEED_EXPORTERS = {  
    'json': 'myproject.settings.CustomJsonLinesItemExporter',  
}  

8.2再次执行,解决items.json出现Export Unicode字符集的问题,items.json路径为\myproject
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值