scrapy的安装

不得姐网站
直接用命令

pip install scrapy

网络可能较慢,等等就好
另外windows环境下你还需要安装win32
我之前就是因为没有安装这个库,白忙活了一天,一直都是报错状态

pip install pypiwin32

scrapy的使用

cd到卓面或者其他看的到的文件夹
一行一行输入命令即可

1、scrapy startproject qsbk2、scrapy genspider bdj_spider budejie.com3、scrapy crawl bdj_spider解释
1、scrapy startproject 项目名2、scrapy genspider 爬虫名 网址(不加www)3、爬虫名
修改爬虫里面的parse函数,改为自己想要的效果即可

以上为例
bdj_spider.py是最重要的文件,在里面修改parse即可获得自己想要的

这是获取百思不得姐第一页用户名的代码

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http.response.html import HtmlResponse
from scrapy.selector.unified import SelectorListclass BdjSpiderSpider(scrapy.Spider):name = 'bdj_spider'allowed_domains = ['budejie.com']start_urls = ['http://budejie.com/']def parse(self, response):print('='*100)print('='*100)print('='*100)words = response.xpath("//div[@class='j-list-user']/div")for word in words:author=word.xpath(".//a/text()").get()print(author)print('='*100)print('='*100)print('='*100)print('='*100)

加等号的目的是更容易看出筛选的内容

进阶

将爬取的文件存储在文件夹里
我遇到个巨大的坑。浪费我好长时间,都怪自己当时教程没看明白,反反复复的找错

需要在上文修改的文件

1、
bdj_spider.py

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http.response.html import HtmlResponse
from scrapy.selector.unified import SelectorListclass BdjSpiderSpider(scrapy.Spider):name = 'bdj_spider'allowed_domains = ['budejie.com']start_urls = ['http://budejie.com/']def parse(self, response):print('='*100)print('='*100)print('='*100)words = response.xpath("//div[@class='j-r-list-c']/div")for word in words:author=word.xpath(".//a/text()").get()author="".join(author).strip()duanzi = {"author":author}print(duanzi)yield duanzi


切记这个yield后面必须有返回的值还必须有缩进一定要是for下面的,否则下面的process_item函数根本不会调用
setting里面改了也没有用

2、
pipelines.py

# -*- coding: utf-8 -*-# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import jsonclass BdjPipeline(object):def __init__(self):self.fp = open("duanzi.json","w",encoding='utf-8')self.fp.write("Hello")def open_spider(self,spider):print('爬虫开始了哈哈哈哈。。。。。')def process_item(self,item,spider):item_json = json.dumps(item,ensure_ascii=False)print("###"*100)print(item_json)self.fp.write(item_json+'\n')return itemdef close_spider(self,spider):self.fp.close()print('爬虫结束了哈哈哈哈。。。。。')

3、
settings.py
把这个解开注释即可,或者复制以下代码覆盖文件

# -*- coding: utf-8 -*-# Scrapy settings for bdj project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.htmlBOT_NAME = 'bdj'SPIDER_MODULES = ['bdj.spiders']
NEWSPIDER_MODULE = 'bdj.spiders'# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'bdj (+http://www.yourdomain.com)'# Obey robots.txt rules
ROBOTSTXT_OBEY = False# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16# Disable cookies (enabled by default)
#COOKIES_ENABLED = False# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {#    'bdj.middlewares.BdjSpiderMiddleware': 543,
#}# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {#    'bdj.middlewares.BdjDownloaderMiddleware': 543,
#}# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {#    'scrapy.extensions.telnet.TelnetConsole': None,
#}# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {'bdj.pipelines.BdjPipeline': 300
}# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

那么看看爬到的数据吧

成功

【Python】Scrapy的安装与使用相关推荐

  1. Python:Scrapy的安装和入门案例

    Scrapy的安装介绍 Scrapy框架官方网址:http://doc.scrapy.org/en/latest Scrapy中文维护站点:http://scrapy-chs.readthedocs. ...

  2. python+scrapy+win10安装和使用

    在安装scrapy 使用时,折腾了我1天时间,特此把该问题作一个记录,供以后或者同样遇到此问题的伙伴使用. 说明:win10 64位系统 安装Twisted 这是安装该模块出现问题的主要问题. 错误类 ...

  3. Python Scrapy 安装及相关配置

    本文仅供学习交流使用,如侵立删!demo下载见文末 Python Scrapy 安装及相关配置 环境 win10 Python:3.6.7 Scrapy:2.4.1 Python 安装

  4. python如何安装scrapy_Python爬虫之Scrapy的安装

    一.Scrapy的介绍 Scrapy是用Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化 ...

  5. python中Scrapy的安装详细过程

    目录 安装python 安装pywin32 安装setuptools 安装twisted 安装zopeinterface 安装pyopenssl 安装twisted 安装lxml 安装w3lib 安装 ...

  6. linux安装python库报错pywin32_Python3爬虫利器:Scrapy的安装

    Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...

  7. Python 爬虫6——Scrapy的安装和使用

    前面我们简述了使用Python自带的urllib和urllib2库完成的一下爬取网页数据的操作,但其实能完成的功能都很简单,假如要进行复制的数据匹配和高效的操作,可以引入第三方的框架,例如Scrapy ...

  8. python爬虫 - scrapy的安装和使用

    http://blog.csdn.net/pipisorry/article/details/45190851 Crawler Framework爬虫框架scrapy简介 Scrapy是Python开 ...

  9. 【Python】关于安装爬虫框架scrapy的感悟

    前景提要: boss看我最近闲得很,决定让我学习一下新知识----python 爬虫 安装过程: 前一周初步了解了一下python语言,给我的感觉是python很逗,像PHP一样,不要需要对变量类型进 ...

  10. python怎么安装scrapy_windows下安装python+scrapy

    最近忽然有了想要学习python爬虫的想法,但是首先需要安装工具.python安装倒是很轻松,只要傻瓜式一键安装即可,但是在Windows下安装scrapy倒不是件容易的事情.言归正传,说下我从昨天下 ...

最新文章

  1. 【动态规划】分组背包
  2. 浏览器了解(二)HTML解析过程
  3. python vtk mousemove_VTK的视点研究之三维空间漫游(转载)
  4. [BC Round#26] Card 【各种水】
  5. svn版本库浏览器_在SVN版本库浏览器中直接编辑文件保存后不会弹..._网络编辑_帮考网...
  6. selenium 基于浏览器驱动测试
  7. CC2530通用I/O
  8. ue4打包安卓发送udp报文_内核udp报文截取、修改和发送
  9. 15款非常有用的前端开发CSS网格(grid system)生成器
  10. phpnow 升级 php 5.4,PHPnow 升级 php 5.4 的方法
  11. 一个QQ盗号木马是这样诞生的(C#)
  12. segment routing详解十一问
  13. 随机数 == 伪随机数?
  14. [Demo]提取个人博客园闪存+评论
  15. 在 SCA Module 中使用 iBATIS 框架实现数据持久层
  16. RAID5破坏的数据恢复流程和思考
  17. Mybatis-增删改查踩坑- attempted to return null from a method with a primitive return type (int).
  18. android xposed如何写,Xposed插件开发入门详解
  19. 模型训练的性能优化方法
  20. weights.getA()是什么?

热门文章

  1. [C++STL]常用算术生成算法
  2. C++ class实现十字链表存储的图(完整代码)
  3. 《C++ Primer》13.1.4节练习
  4. 蓝桥杯2016初赛-有奖猜谜-模拟
  5. 数据结构与算法--复杂链表的复制
  6. 数据结构(哈夫曼树,哈夫曼编码)入门篇,JAVA实现
  7. Web Service和Servlet的区别
  8. #3551. [ONTAK2010]Peaks加强版(kruskal 重构树 + 主席树)
  9. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes kmp + dp
  10. 【NOIP模拟】彩色树【树形dp】【树链剖分性质】【复杂度分析】