scrapy.py
1.cmd运行scrapy shell http://www.baidu.com response.xpath('//div[@aa="bb"]') 找到需要匹配的内容 ##仅供参考语法,内容不准确 2.cmd运行: scrapy startproject sunbeam(名字随意) 然后在pycharm打开项目sunbeam 3.在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
class MyspiderItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
create_time = scrapy.Field()
content = scrapy.Field()
digg_count = scrapy.Field()
favorite_count = scrapy.Field()
comment_count = scrapy.Field()
author = scrapy.Field()

4.在cmd运行scrapy genspider aaa,这时在pytharm的spiders文件夹下会生成一个aa.py文件(或者手动新建也可以),然后编辑此文件: # -*- coding: utf-8 -*-
import scrapy
import time
import json
from myspider.items import MyspiderItem

class NhsqSpider(scrapy.Spider):
name = 'nhsq' #名字必须唯一
allowed_domains = ['neihanshequ.com']
#第一种方法,start_urls必须是序列或元祖,不能是字符串
start_urls = ['http://neihanshequ.com/']
#第二种方法,如果不写start_urls就必须写start_requests方法

def start_requests(self):
url = 'http://neihanshequ.com/joke/?is_json=1&app_name=neihanshequ_web&max_time={}'.format(int(time.time()))
yield scrapy.Request(url,callback=self.parse)

def parse(self, response):
items = MyspiderItem()
result = json.loads(response.text)
data = result.get('data').get('data')
for i in range(20):
items['content'] = data[i].get('group').get('content')
items['create_time'] = data[i].get('group').get('create_time')

yield items #yield以后数据返回到了pipelines.py
''' yield scrapy.Request(link,callback=self.parse_item)
def
''' ## 对返回数据做处理 1.在settings.py把ITEM_PIPELINES=....这一行的注释取消 然后在pipelinse编辑: # -*- coding: utf-8 -*-

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

class SunbeamPipeline(object):
def __init__(self):
self.file = open('duanzi.json','w')

def process_item(self, item, spider):
#print(item['content'])
#content = json.dumps(dict(item),ensure_ascii=False)+"\n"
content = json.dumps(dict(item))+"\n"
print(content)

self.file.write(content)
#return item
​ 然后在cmd运行scrapy crawl aaa

scrapy爬取段子的更多相关文章

  1. Scrapy爬取美女图片 (原创)

    有半个月没有更新了,最近确实有点忙.先是华为的比赛,接着实验室又有项目,然后又学习了一些新的知识,所以没有更新文章.为了表达我的歉意,我给大家来一波福利... 今天咱们说的是爬虫框架.之前我使用pyt ...

  2. 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...

  3. scrapy爬取西刺网站ip

    # scrapy爬取西刺网站ip # -*- coding: utf-8 -*- import scrapy from xici.items import XiciItem class Xicispi ...

  4. scrapy爬取豆瓣电影top250

    # -*- coding: utf-8 -*- # scrapy爬取豆瓣电影top250 import scrapy from douban.items import DoubanItem class ...

  5. scrapy爬取极客学院全部课程

    # -*- coding: utf-8 -*- # scrapy爬取极客学院全部课程 import scrapy from pyquery import PyQuery as pq from jike ...

  6. scrapy爬取全部知乎用户信息

    # -*- coding: utf-8 -*- # scrapy爬取全部知乎用户信息 # 1:是否遵守robbots_txt协议改为False # 2: 加入爬取所需的headers: user-ag ...

  7. Scrapy爬取Ajax(异步加载)网页实例——简书付费连载

    这两天学习了Scrapy爬虫框架的基本使用,练习的例子爬取的都是传统的直接加载完网页的内容,就想试试爬取用Ajax技术加载的网页. 这里以简书里的优选连载网页为例分享一下我的爬取过程. 网址为: ht ...

  8. Python网络爬虫与如何爬取段子的项目实例

    一.网络爬虫 Python爬虫开发工程师,从网站某一个页面(通常是首页)开始,读取网页的内容,找到在网页中的其它链接地址,然后通过这些链接地址寻找下一个网页,这样一直循环下去,直到把这个网站所有的网页 ...

  9. Scrapy爬取静态页面

    Scrapy爬取静态页面 安装Scrapy框架: Scrapy是python下一个非常有用的一个爬虫框架 Pycharm下: 搜索Scrapy库添加进项目即可 终端下: #python2 sudo p ...

随机推荐

  1. 裸机——LCD

    1.先了解显示器们 CRT显示器 原理,使用电子轰击荧光粉实现显示. 特点,主动发光,颜色绚丽,但是大. 等离子显示器(PDP) 原理,在显示屏上排列上千个密封的小低压气体室,通过电流激发使其发出肉眼 ...

  2. python基础之生成器表达式形式、面向过程编程、内置函数部分

    生成器表达式形式 直接上代码 1 # yield的表达式形式 2 def foo(): 3 print('starting') 4 while True: 5 x=yield #默认返回为空,实际上为 ...

  3. Git-Git里程碑

    里程碑即Tag,是人为对提交进行的命名.这和Git的提交ID是否太长无关,使用任何数字版本号无论长短,都没有使用一个直观的表意的字符串来得方便.例如:用里程碑名称"v2.1"对应于 ...

  4. Hive 压缩技术Data Compression

    Mapreducwe 执行流程 :input > map > shuffle > reduce > output 压缩执行时间,map 之后,压缩,数据存储在本地磁盘,减少磁盘 ...

  5. 15.5,centos下redis安全相关

      博文背景: 由于发现众多同学,在使用云服务器时,安装的redis3.0+版本都关闭了protected-mode,因而都遭遇了挖矿病毒的攻击,使得服务器99%的占用率!! 因此我们在使用redis ...

  6. hive原理

    什么是Hive Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能. Hive架构图 Jobtracker是hadoop1.x中的组件,它的 ...

  7. X的N次方。N比较大。

    final static long DIV = 1000000009; //分治法, 注意java类型为long, C++为__int64或 long long public static long ...

  8. 写一个quick sort

    #include <stdio.h> #include <stdlib.h> //int a[]={1000,10000,9,10,30,20,50,23,90,100,10} ...

  9. HTML简易学习笔记

    文字版地址 https://github.com/songzhenhua/github/blob/master/HTML简易学习笔记.txt

  10. ASP.NET Core 2.1 源码学习之 Options[2]:IOptions 【转】

    原文链接:https://www.cnblogs.com/RainingNight/p/strongly-typed-options-ioptions-in-asp-net-core.html 在 上 ...