1、引言

  Scrapy框架结构清晰,基于twisted的异步架构可以充分利用计算机资源,是做爬虫必备基础,本文将对Scrapy的安装作介绍。

2、安装lxml

  2.1  下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted  选择对应python3.5的lxml库

2.2 如果pip的版本过低,先升级pip:

python -m pip install -U pip

2.3 安装lxml库(先将下载的库文件copy到python的安装目录,按住shift键并鼠标右击选择“在此处打开命令窗口”)

pip install lxml-4.1.1-cp35-cp35m-win_amd64.whl

看到出现successfully等字样说明按章成功。

3、 安装Twisted库

3.1 下载链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted  选择对应python3.5的库文件

  

3.2 安装

pip install Twisted-17.9.0-cp35-cp35m-win_amd64.whl

  看到出现successfully等字样说明按章成功。

Note:部分机器可能安装失败,可以尝试将 Twisted-17.9.0-cp35-cp35m-win_amd64.whl文件移动到  $python/Scripts/   目录下,重新安装。

 

4、安装Scrapy

twisted库安装成功后,安装scrapy就简单了,在命令提示符窗口直接输入命令:

pip install scrapy

  看到出现successfully等字样说明按章成功。

5、Scrapy测试

5.1 新建项目

  先新建一个Scrapy爬虫项目,选择python的工作目录(我的是:H:\PycharmProjects   然后安装Shift键并鼠标右键选择“在此处打开命令窗口”),然后输入命令:

scrapy startproject allister

  

  对应目录会生成目录allister文件夹,目录结构如下:

└── allister
├── allister
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
└── scrapy.cfg 简单介绍个文件的作用:
# -----------------------------------------------
scrapy.cfg:项目的配置文件;
allister/ : 项目的python模块,将会从这里引用代码
allister/items.py:项目的items文件
allister/pipelines.py:项目的pipelines文件
allister/settings.py :项目的设置文件
allister/spiders : 存储爬虫的目录

5.2 修改allister/items.py文件:

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

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html import scrapy class AllisterItem(scrapy.Item):
name = scrapy.Field()
level = scrapy.Field()
info = scrapy.Field()

  

5.3 编写文件 AllisterSpider.py

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : AllisterSpider.py
# @Author: Allister.Liu
# @Date : 2018/1/18
# @Desc : import scrapy
from allister.items import AllisterItem class ItcastSpider(scrapy.Spider):
name = "ic2c"
allowed_domains = ["http://www.itcast.cn"]
start_urls = [
"http://www.itcast.cn/channel/teacher.shtml#ac"
] def parse(self, response):
items = [] for site in response.xpath('//div[@class="li_txt"]'):
item = AllisterItem() t_name = site.xpath('h3/text()')
t_level = site.xpath('h4/text()')
t_desc = site.xpath('p/text()') unicode_teacher_name = t_name.extract_first().strip()
unicode_teacher_level = t_level.extract_first().strip()
unicode_teacher_info = t_desc.extract_first().strip() item["name"] = unicode_teacher_name
item["level"] = unicode_teacher_level
item["info"] = unicode_teacher_info yield item

  

编写完成后复制至项目的 \allister\spiders目录下,cmd选择项目根目录输入以下命令:  

scrapy crawl ic2c -o itcast_teachers.json -t json

  抓取的数据将以json的格式存储在ic2c_infos.json文件中;

如果出现如下错误请看对应解决办法:

Scrapy运行错误:ImportError: No module named win32api

Python3.5下安装&测试Scrapy的更多相关文章

  1. Python3.X下安装Scrapy

    Python3.X下安装Scrapy (转载) 2017年08月09日 15:19:30 jingzhilie7908 阅读数:519 标签: python   相信很多同学对于爬虫需要安装Scrap ...

  2. centos7 python3.5 下安装paramiko

    centos7 python3.5 下安装paramiko 安装开发包 yum install openssl openssl-devel python-dev -y 安装pip前需要前置安装setu ...

  3. 在python3.5下安装scrapy包

    此前scrapy只支持python2.x 但是最新的1.1.0rc1已结开始支持py3了 如果电脑上安装了scrapy的依赖包,诸如lxml.OpenSSL 1.你直接下载Scrapy-1.1.0rc ...

  4. python3 linux下安装

    1.下载 https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz 2.安装 上传到linux服务器 #进入上传文件的目录 cd /app/pr ...

  5. 在Python3.5下安装和测试Scrapy爬网站

    1. 引言 Scrapy框架结构清晰,基于twisted的异步架构可以充分利用计算机资源,是爬虫做大的必备基础.本文将讲解如何快速安装此框架并使用起来. 2. 安装Twisted 2.1 同安装Lxm ...

  6. win7中python3.4下安装scrapy爬虫框架(亲测可用)

    貌似最新的scrapy已经支持python3,但是错误挺多的,以下为在win7中的安装步骤: 1.首先需要安装Scrapy的依赖包,包括parsel, w3lib, cryptography, pyO ...

  7. 在Windows10 64位 Anaconda4 Python3.5下安装XGBoost

    系统环境: Windows10 64bit Anaconda4 Python3.5.1 软件安装: Git for Windows MINGW 在安装的时候要改一个选择(Architecture选择x ...

  8. Windows python3.3下安装BeautifulSoup

    首先在官网下载:http://www.crummy.com/software/BeautifulSoup/#Download BeautifulSoup在版本4以上都开始支持python3了,所以就下 ...

  9. 关于在Python3.6下安装MySQL-python,flask-sqlalchemy模块的问题

    这周末在学习Flask框架的时候,有需要安装MySQL-python模块,一开始用pip安装: pip install MySQL-python 但是安装的时候报错了: error: command ...

随机推荐

  1. 《深入浅出Netty》【PDF】下载

    <深入浅出Netty>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062563 内容简介 本文档主要讲述的是深入浅出Netty: ...

  2. OC学习16——对象归档

    转载自  OC学习篇之---归档和解挡 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOutputStream来进行操作的.当然在操作的这些对象都 ...

  3. bzoj 2752: [HAOI2012]高速公路(road)

    Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站.Y901高速公路是一条由N-1段路以及N个收 ...

  4. java方式连接数据操作数据库

    package com.bdqn.dao.impl; import java.io.IOException;import java.io.InputStream;import java.io.Seri ...

  5. Logback分别打印info日志和error日志

    <?xml version="1.0" encoding="utf-8" ?><configuration> <appender ...

  6. 删除SVN版本信息的两种方式

    一.在linux下删除SVN版本信息 删除这些目录是很简单的,命令如下 find . -type d -name ".svn"|xargs rm -rf 或者 find . -ty ...

  7. python的sorted函数

    sorted很简单,没太多好写的 ,只是给自己做个笔记. sorted接受三个参数,返回一个排序之后的list. 第一个接受一个可迭代的对象(因为sorted实现了迭代协议,所以接受的参数不一定需要l ...

  8. Class StatusesTableSeeder does not exist 如何解决

    Class StatusesTableSeeder does not exist错误如何解决 Laravel 5.* 执行seeder命令出现错误的解决方法     最近在使用Laravel开发一个项 ...

  9. 初用MssqlOnLinux 【1】

    https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat 使用 Centos7,NetCore2.0, ...

  10. 移动端H5页面惯性滑动监听

    移动端H5页面惯性滑动监听 在移动端,当你快速滑动有滚动条的页面时,当你手指离开屏幕时,滚动条并不会立即停止,而是会随着"惯性"继续滑动一段距离. 在做项目的过程中,需要监听惯性滑 ...