爬虫可以发送给引擎的两种请求:

    # 1、url:# (爬虫)yield scrapy.Request -> 引擎 -> 调度器(发送给调度器入队) -> 引擎(调度器出队请求于引擎)# -> 下载器(引擎发送于下载器) -> 引擎(下载器成功(失败)返回引擎):-> 爬虫(引擎接收成功将给爬虫response)or -> 调度器(失败给调度器重新下载)# -> 引擎(爬虫接收response并做处理,发送跟进url:yield scrapy.Request) -> 调度器(引擎发送给调度器入队) ->...# 2、weiboitem:# 一般在接收response后便有了数据,然后# (爬虫) yield weiboitem -> 引擎 -> pipelines(管道)进行存储,管道中自己写存储代码 -> mysql or redis

一、准备工作

  • python、pip、scrapy(pip install Scrapy)
  • 测试:scrapy fetch http://www.baidu.com

二、构建crapy框架

  • 创建爬虫项目(cmd或terminal):scrapy startproject mySpiderName
  • cd:cd mySpider
  • 创建爬虫:scrapy genspider myspidername www.dytt8.net
    ( www.dytt8.net 是要爬取网址的根域名,只有在此根域名才能爬取到内容)
  • 修改settings协议: ROBOTSTXT_OBEY = False
  • 切记在settings中ITEM_PIPELINES列表添加语句(打开注释),否则管道不会被执行:
    ‘mySpiderName.pipelines.WeiboSpiderPipeline’: 300,

三、填写代码三部曲

  • 在自动生成的spiders文件夹下的myspider.py文件中编辑:
import scrapy
from hotnewsSpider.items import WeiboSpiderItem     # hotnewsSpider为项目名,WeiboSpiderItem为爬虫item类,在items.py中可找到# 创建第二个爬虫时需要手动在items中添加此类from bs4 import BeautifulSoupclass WeiboSpider(scrapy.Spider):# 以微博为例:name = 'weibo'                          # 爬虫名 -- 自动生成,唯一,不可变allowed_domains = ['s.weibo.com']       # 允许访问的根域名start_urls = ['http://s.weibo.com/']    # 起始访问地址searchName = "张钧甯 感谢抬爱"headers = {}cookies = {}urls = [# 模拟搜索 searchName"https://s.weibo.com/weibo?q=%s&Refer=SWeibo_box"%searchName]# urls.extend(start_urls)# 重写起始请求,可以给请求加上许多信息def start_requests(self):# 发送初始请求for url in self.urls:yield scrapy.Request(url=url, headers=self.headers, cookies=self.cookies, callback=self.parse)# 默认第一次返回response接收函数,第二次response可以继续返回这里,也可以返回你定义的人一个函数中,# 这在yield scrapy.Request(url,callback=self.your_parse)中决定def parse(self, response):# 用爬虫对应的item类声明一个对象,类型为字典,用来保存数据,通过 yield weiboitem 返回给引擎weiboitem = WeiboSpiderItem()                       # from hotnewsSpider.items import WeiboSpiderItem# BeautifulSoup代码块:html = response.textsoup = BeautifulSoup(html, 'lxml')content_id_div = soup.find(id='pl_feedlist_index')card_wraps = content_id_div.find_all(class_='card-wrap')id = 0for card_wrap_item in card_wraps:# 用户名username = card_wrap_item.find(class_='info').find(class_='name').text# 用户头像user_headimg = card_wrap_item.find(class_='avator').find('img')['src']# 内容# 文字 偶尔会搜索出某个人content_text_html = card_wrap_item.find(class_='txt')content_text = ''if content_text_html:content_text = content_text_html.get_text().replace(' ', '').replace('\n', '').replace('展开全文c', '')# 图片 有的无图img_items_html = card_wrap_item.find(class_='m3')content_imgs = []if img_items_html:for img_item in img_items_html.find_all('img'):content_imgs.append(img_item['src'])# (收藏)、转发、评论、点赞数量other_items_html = card_wrap_item.find(class_='card-act')other_items_dic = {}if other_items_html:other_items_lst = other_items_html.find_all('a')for other_item_index in range(len(other_items_lst)):if other_item_index == 0:other_items_dic['收藏'] = ""elif other_item_index == 1:other_items_dic['转发'] = other_items_lst[other_item_index].text.strip().split()[1]elif other_item_index == 2:other_items_dic['评论'] = other_items_lst[other_item_index].text.strip().split()[1]else:other_items_dic['点赞'] = other_items_lst[other_item_index].text.strip()# print(other_items_dic)id += 1weiboitem['id'] = idweiboitem['username'] = usernameweiboitem['user_headimg'] = user_headimgweiboitem['content_text'] = content_textweiboitem['content_imgs'] = content_imgsweiboitem['other_items_dic'] = other_items_dicyield weiboitem      # 返回数据给引擎,引擎将其传入管道执行管道中的代码# yield scrapy.Request(url,callback=self.parse)    # 返回跟进url给引擎# yield scrapy.Request(url,callback=self.parse2)    # 返回跟进url给引擎break       # 用于测试,只拿一次数据def parse2(self,response):pass
  • 在items.py中初始化item字典(第二次以上新建的爬虫需要自己新增对应类)
import scrapy# 第一个爬虫对应的item类,在创建项目时自动产生
class HotnewsspiderItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()pass# 自己新增的爬虫类
class WeiboSpiderItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()id = scrapy.Field()username = scrapy.Field()user_headimg = scrapy.Field()content_text = scrapy.Field()content_imgs = scrapy.Field()other_items_dic = scrapy.Field()pass
  • 在pipelines.py中保存数据
# 第一个爬虫对应的Pipeline类,在创建项目时自动产生
class HotnewsspiderPipeline(object):def process_item(self, item, spider):pass# return item# 自己新增的爬虫类
# 切记在settings中ITEM_PIPELINES列表添加语句,否则不会被执行:
# 'hotnewsSpider.pipelines.WeiboSpiderPipeline': 300,
class WeiboSpiderPipeline(object):def process_item(self, item, spider):# 在这里将数据存入mysql,redisprint(item)

运行:

  • scrapy crawl mysipdername(别忘了cd目录)
  • 添加以下任意一个py运行文件命名run或main,要与scrapy.cfg文件同级目录
    更改自己的爬虫名即可右键运行
一、
from scrapy.cmdline import execute
import sys
import os'''
运行scrapy爬虫的方式是在命令行输入    scrapy crawl <spider_name>
调试的常用方式是在命令行输入          scrapy shell <url_name>
'''sys.path.append(os.path.dirname(os.path.abspath(__file__)))execute(['scrapy', 'crawl', 'weibo'])  # 你需要将此处的spider_name替换为你自己的爬虫名称
二、
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings'''
运行scrapy爬虫的方式是在命令行输入    scrapy crawl <spider_name>
调试的常用方式是在命令行输入          scrapy shell <url_name>
'''if __name__ == '__main__':process = CrawlerProcess(get_project_settings())process.crawl('weibo')    #  你需要将此处的spider_name替换为你自己的爬虫名称process.start()

用scrapy框架写爬虫相关推荐

  1. 关于使用scrapy框架编写爬虫以及Ajax动态加载问题、反爬问题解决方案

    关于使用scrapy框架编写爬虫以及Ajax动态加载问题.反爬问题解决方案 参考文章: (1)关于使用scrapy框架编写爬虫以及Ajax动态加载问题.反爬问题解决方案 (2)https://www. ...

  2. 一个scrapy框架的爬虫(爬取京东图书)

    我们的这个爬虫设计来爬取京东图书(jd.com). scrapy框架相信大家比较了解了.里面有很多复杂的机制,超出本文的范围. 1.爬虫spider tips: 1.xpath的语法比较坑,但是你可以 ...

  3. python爬虫之使用Scrapy框架编写爬虫

    转自:http://www.jb51.net/article/57183.htm 前面的文章我们介绍了Python爬虫框架Scrapy的安装与配置等基本资料,本文我们就来看看如何使用Scrapy框架方 ...

  4. python爬取京东书籍_一个scrapy框架的爬虫(爬取京东图书)

    我们的这个爬虫设计来爬取京东图书(jd.com). scrapy框架相信大家比较了解了.里面有很多复杂的机制,超出本文的范围. 1.爬虫spider tips: 1.xpath的语法比较坑,但是你可以 ...

  5. python3 scrapy框架,Python3爬虫(十八) Scrapy框架(二)

    对Scrapy框架(一)的补充 Infi-chu: Scrapy优点: 提供了内置的 HTTP 缓存 ,以加速本地开发 . 提供了自动节流调节机制,而且具有遵守 robots.txt 的设置的能力. ...

  6. Scrapy框架简单爬虫demo

    一.导读 接着上一节的Scrapy环境搭建,这次我们开始长征的第二步,如果第一步的还没走,请出门右转(Scrapy爬虫框架环境搭建) 二.步骤 新建Scrapy项目 项目名称是scrapyDemo s ...

  7. Python:Pycharm如何使用scrapy框架做爬虫?

    因为入门python以来一直使用pycharm,所以对着黑白的DOS不习惯,所以此次来实现使用pycharm进行实现使用scrapy框架 ①pip install scrapy(首先安装scrapy第 ...

  8. 解析python网络爬虫pdf 黑马程序员_正版 解析Python网络爬虫 核心技术 Scrapy框架 分布式爬虫 黑马程序员 Python应用编程丛书 中国铁道出版社...

    商品参数 书名:Python应用编程丛书:解析Python网络爬虫:核心技术.Scrapy框架.分布式爬虫 定价:52.00元 作者:[中国]黑马程序员 出版社:中国铁道出版社 出版日期:2018-0 ...

  9. pythonscrapy爬虫_零基础写python爬虫之使用Scrapy框架编写爬虫

    网络爬虫,是在网上进行数据抓取的程序,使用它能够抓取特定网页的HTML数据.虽然我们利用一些库开发一个爬虫程序,但是使用框架可以大大提高效率,缩短开发时间.Scrapy是一个使用Python编写的,轻 ...

最新文章

  1. mysql 基于集_一种基于记录集查找特定行的方法_MySQL
  2. 精通python-轻松打造11周精通python计划(完结) | 软件库
  3. Hdoj Minimize The Difference
  4. pixhawk硬件构架
  5. 使用代码获得ABAP software component的version
  6. 使用Optuna的XGBoost模型的高效超参数优化
  7. MFc消息映射机制理解
  8. STL容器删除元素的陷阱
  9. 笔记本电脑如何强制关机_如果你的MacBook一直关机,该怎么办?
  10. 创建表空间和创建表过程分析
  11. ubuntu升级tensorflow版本
  12. @Zabbix配置snmptrap及使用snmptt解析格式化输出
  13. Vue开发环境搭建详解
  14. 经验模态分解股票波动matlab,matlab经验模态分解程序
  15. 2019年全国大学生电子竞赛 | 电磁炮制作教程
  16. 杭州电子科技大学计算机学院院长,杭州电子科技大学计算机学院导师教师师资介绍简介-彭勇...
  17. 【默认输入法】Android8.1默认输入法修改(解决输入法切换和权限获取等问题)
  18. 腾讯云从良心云转变成“凉心云”,乱封禁服务器与域名怎么办?
  19. 硅谷华人AI精英大批回国成趋势,BAT在美设「挖人」据点,猎头暗中潜伏
  20. KO data-bind=“click: func“函数自动执行问题

热门文章

  1. 良性计算机病毒对计算有没有危害机系统,154、计算机病毒有良性和恶性之分,其中, – 手机爱问...
  2. ceph 存储 对比_分布式存储系统 Curve
  3. hp-ux锁定用户密码_UX设计101:用户研究-入门需要了解的一切
  4. java spring cloud版b2b2c社交电商spring cloud分布式微服务:服务注册与发现(Eureka、Consul)...
  5. 一行代码实现底部导航栏TabLayout
  6. [USACO 4.2] 完美的牛栏
  7. vs2015提示中文
  8. 第一轮复习完毕,kmp走起
  9. 几个想法,有兴趣的可以深入下去
  10. ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、Server.MapPath的区别...