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

Scrapy吸引人的地方在于它是一个框架,任何人都可以根据需求方便的修改。它也提供了多种类型爬虫的基类,如BaseSpider、sitemap爬虫等,最新版本又提供了web2.0爬虫的支持。

使用python2.7.11

https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz

解压

#tar -xvf Python-2.7.11.tgz

cd Python-2.7.11

多版本python存在才进行修改

报错:

exceptions.ImportError: No module named _sqlite3

下载:

https://pypi.python.org/pypi/pysqlite/

tar -zxvf pysqlite-2.8.1.tar.gz

cd pysqlite-2.8.1

python setup.py install

先修改Python-2.7.11目录里的setup.py 文件:

在下面这段的下一行添加’/usr/local/lib/sqlite3/include’,

sqlite_inc_paths = [ ‘/usr/include’,

‘/usr/include/sqlite’,

‘/usr/include/sqlite3’,

‘/usr/local/include’,

‘/usr/local/include/sqlite’,

‘/usr/local/include/sqlite3’,

‘/usr/local/lib/sqlite3/include’,

安装

#./configure

#make all

#make install

#make clean

#make distclean

查看版本信息

#/usr/local/bin/python2.7 -V

建立软连接,使系统默认的 python指向 python2.7

#mv /usr/bin/python /usr/bin/python2.6.6

#ln -s /usr/local/bin/python2.7 /usr/bin/python

7.重新检验Python 版本

#python -V

解决系统 Python 软链接指向 Python2.7 版本后,因为yum是不兼容 Python 2.7的,所以yum不能正常工作,我们需要指定 yum 的Python版本

#vi /usr/bin/yum

将文件头部的

#!/usr/bin/python

改成

#!/usr/bin/python2.6.6

下载pip

https://pip.pypa.io/en/latest/installing/

python get-pip.py

[root@testserver4 ~]# python get-pip.py

Collecting pip

Downloading pip-7.1.2-py2.py3-none-any.whl (1.1MB)

100% |████████████████████████████████| 1.1MB 350kB/s

Collecting wheel

Downloading wheel-0.26.0-py2.py3-none-any.whl (63kB)

100% |████████████████████████████████| 65kB 5.0MB/s

Collecting argparse (from wheel)

Downloading argparse-1.4.0-py2.py3-none-any.whl

Installing collected packages: pip, argparse, wheel

Successfully installed argparse-1.4.0 pip-7.1.2 wheel-0.26.0

/tmp/tmpcBdh5G/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90:

InsecurePlatformWarning: A true SSLContext object is not available.

This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail.

For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

报错:

出现:zipimport.ZipImportError: can't decompress data; zlib not available错误

解决办法重新编译一下Python源码安装包,如下:

tar zxvf Python-2.7.11.tgz

cd Python-2.7.11

./configure

vi Modules/Setup

在这里把454行左右的 找到

#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

去掉注释

zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

make

make install

报错:ImportError:cannot import name HTTPSHandler

解决:

yum install -y openssl openssl-devel

然后重新编译python

pip install urllib3

使用pip安装:

pip install Scrapy

提示成功

Successfully installed Scrapy-1.0.3 characteristic-14.3.0 lxml-3.5.0 pyasn1-modules-0.0.8 service-identity-14.0.0

卸载软件包

pip uninstall Scrapy

列出软件包清单

pip list

更新pip

pip install -U pip

报错

/usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90:

InsecurePlatformWarning: A true SSLContext object is not available.

This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail.

For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.

首先安装python-devel libffi-devel openssl-devel

yum install python-devel libffi-devel openssl-devel

之后在安装pyopenssl ndg-httpsclient pyasn1

pip install pyopenssl ndg-httpsclient pyasn1

报错

Collecting Twisted>=10.0.0 (from Scrapy)

Could not find a version that satisfies the requirement Twisted>=10.0.0 (from Scrapy) (from versions: )

Some externally hosted files were ignored as access to them may be unreliable (use --allow-external Twisted to allow).

No matching distribution found for Twisted>=10.0.0 (from Scrapy)

安装Twisted

wget http://twistedmatrix.com/Releases/Twisted/15.5/Twisted-15.5.0.tar.bz2

安装Twisted

下载好Twisted后,进入到下载目录,解压:

[root@codebreaker ~]#tar -jvxf Twisted-15.5.0.tar.bz2

解压完成后进入相应目录:

[root@codebreaker ~]#cd Twisted-15.5.0

执行安装:

[root@codebreaker Twisted-15.5.0]#python setup.py install

安装完成后进入python,测试Twisted是否安装成功

[root@codebreaker Twisted-15.5.0]# python

>>> import twisted

如果没有错误发生,说明Twisted已经安装成功了

报错:

error: command 'gcc' failed with exit status 1

yum install python python-dev* python-lxml* libxml2-dev* libxslt-dev*

建立项目:

scrapy startproject itzhaopin

路径 /root/Twisted-15.5.0/itzhaopin/itzhaopin

.

├── itzhaopin

│ ├── itzhaopin

│ │ ├── __init__.py

│ │ ├── items.py

│ │ ├── pipelines.py

│ │ ├── settings.py

│ │ └── spiders

│ │ └── __init__.py

│ └── scrapy.cfg

scrapy.cfg: 项目配置文件

items.py: 需要提取的数据结构定义文件

pipelines.py:管道定义,用来对items里面提取的数据做进一步处理,如保存等

settings.py: 爬虫配置文件

spiders: 放置spider的目录

例子代码(以下配置内容以源码配置为准):

https://github.com/maxliaops/scrapy-itzhaopin

定义Item

在items.py里面定义我们要抓取的数据:

from scrapy.item import Item, Field

class TencentItem(Item):

name = Field()

catalog = Field()

workLocation = Field()

recruitNumber = Field()

detailLink = Field()

publishTime = Field()

解释:

name = Field() # 职位名称

catalog = Field() # 职位类别

workLocation = Field() # 工作地点

recruitNumber = Field() # 招聘人数

detailLink = Field() # 职位详情页链接

publishTime = Field() # 发布时间

实现Spider

Spider是一个继承自scrapy.contrib.spiders.CrawlSpider的Python类,有三个必需的定义的成员

name: 名字,这个spider的标识

start_urls:一个url列表,spider从这些网页开始抓取

parse():一个方法,当start_urls里面的网页抓取下来之后需要调用这个方法解析网页内容,同时需要返回下一个需要抓取的网页,或者返回items列表

所以在spiders目录下新建一个vi spider/tencent_spider.py:

import re

import json

from scrapy.selector import Selector

try:

from scrapy.spider import Spider

except:

from scrapy.spider import BaseSpider as Spider

from scrapy.utils.response import get_base_url

from scrapy.utils.url import urljoin_rfc

from scrapy.contrib.spiders import CrawlSpider, Rule

from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor as sle

from itzhaopin.items import *

from itzhaopin.misc.log import *

class TencentSpider(CrawlSpider):

name = "tencent"

allowed_domains = ["tencent.com"]

start_urls = [

"http://hr.tencent.com/position.php"

]

rules = [ # 定义爬取URL的规则

Rule(sle(allow=("/position.php\?&start=\d{,4}#a")), follow=True, callback='parse_item')

]

def parse_item(self, response): # 提取数据到Items里面,主要用到XPath和CSS选择器提取网页数据

items = []

sel = Selector(response)

base_url = get_base_url(response)

sites_even = sel.css('table.tablelist tr.even')

for site in sites_even:

item = TencentItem()

item['name'] = site.css('.l.square a').xpath('text()').extract()

relative_url = site.css('.l.square a').xpath('@href').extract()[0]

item['detailLink'] = urljoin_rfc(base_url, relative_url)

item['catalog'] = site.css('tr > td:nth-child(2)::text').extract()

item['workLocation'] = site.css('tr > td:nth-child(4)::text').extract()

item['recruitNumber'] = site.css('tr > td:nth-child(3)::text').extract()

item['publishTime'] = site.css('tr > td:nth-child(5)::text').extract()

items.append(item)

#print repr(item).decode("unicode-escape") + '\n'

sites_odd = sel.css('table.tablelist tr.odd')

for site in sites_odd:

item = TencentItem()

item['name'] = site.css('.l.square a').xpath('text()').extract()

relative_url = site.css('.l.square a').xpath('@href').extract()[0]

item['detailLink'] = urljoin_rfc(base_url, relative_url)

item['catalog'] = site.css('tr > td:nth-child(2)::text').extract()

item['workLocation'] = site.css('tr > td:nth-child(4)::text').extract()

item['recruitNumber'] = site.css('tr > td:nth-child(3)::text').extract()

item['publishTime'] = site.css('tr > td:nth-child(5)::text').extract()

items.append(item)

#print repr(item).decode("unicode-escape") + '\n'

info('parsed ' + str(response))

return items

def _process_request(self, request):

info('process ' + str(request))

return request

实现PipeLine

PipeLine用来对Spider返回的Item列表进行保存操作,可以写入到文件、或者数据库等。

PipeLine只有一个需要实现的方法:process_item,例如我们将Item保存到JSON格式文件中:

vi pipelines.py

from scrapy import signals

import json

import codecs

class JsonWithEncodingTencentPipeline(object):

def __init__(self):

self.file = codecs.open('tencent.json', 'w', encoding='utf-8')

def process_item(self, item, spider):

line = json.dumps(dict(item), ensure_ascii=False) + "\n"

self.file.write(line)

return item

def spider_closed(self, spider):

self.file.close(

)

设置:

vi settings.py

# Scrapy settings for itzhaopin project

#

# For simplicity, this file contains only the most important settings by

# default. All the other settings are documented here:

#

# http://doc.scrapy.org/en/latest/topics/settings.html

#

BOT_NAME = 'itzhaopin'

SPIDER_MODULES = ['itzhaopin.spiders']

NEWSPIDER_MODULE = 'itzhaopin.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent

#USER_AGENT = 'itzhaopin (+http://www.yourdomain.com)'

ITEM_PIPELINES = {

'itzhaopin.pipelines.JsonWithEncodingTencentPipeline': 300,

}

LOG_LEVEL = 'INFO'

创建目录misc

#vi log.py

from scrapy import log

def warn(msg):

log.msg(str(msg), level=log.WARNING)

def info(msg):

log.msg(str(msg), level=log.INFO)

def debug(msg):

log.msg(str(msg), level=log.DEBUG)

#vi __init__.py (必须要这个文件,内容为空)

到现在,我们就完成了一个基本的爬虫的实现,可以输入下面的命令来启动这个Spider:

#scrapy crawl tencent

爬虫运行结束后,在当前目录下将会生成一个名为tencent.json的文件,其中以JSON格式保存了职位招聘信息。

部分内容如下:

{"recruitNumber": ["1"], "name": ["SD5-资深手游策划(深圳)"], "detailLink": "http://hr.tencent.com/position_detail.php?id=15626&keywords=&tid=0&lid=0", "publishTime": ["2014-04-25"], "catalog": ["产品/项目类"], "workLocation": ["深圳"]}

{"recruitNumber": ["1"], "name": ["TEG13-后台开发工程师(深圳)"], "detailLink": "http://hr.tencent.com/position_detail.php?id=15666&keywords=&tid=0&lid=0", "publishTime": ["2014-04-25"], "catalog": ["技术类"], "workLocation": ["深圳"]}

参考:

http://scrapy.org/

http://blog.csdn.net/HanTangSongMing/article/details/24454453

http://blog.siliconstraits.vn/building-web-crawler-scrapy/

http://blog.csdn.net/olanlanxiari/article/details/48086917

阅读(3767) | 评论(0) | 转发(0) |

python怎么安装scrapy_Python-2.7安装Scrapy 1.0爬虫实例相关推荐

  1. python2.7安装scrapy_python 2.7 的Scrapy安装介绍

    一. Scrapy简介 Scrapy is a fast high-level screen scraping and web crawling framework, used to crawl we ...

  2. python 创建一个空向量,python 创建一个空向量_python 创建一维的0向量实例

    python 创建一维的0向量实例 第一种方法: A=[0]*8 第二种方法: import numpy as np A=np.zeros(8) 以上这篇python 创建一维的0向量实例就是小编分享 ...

  3. python 创建一个空向量_python 创建一维的0向量实例

    python 创建一维的0向量实例 第一种方法: A=[0]*8 第二种方法: import numpy as np A=np.zeros(8) 以上这篇python 创建一维的0向量实例就是小编分享 ...

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

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

  5. 网络爬虫python实例视频-Python网络爬虫实例教程 视频讲解版

    章网络爬虫概述1 1.1认识网络爬虫1 1.1.1网络爬虫的含义1 1.1.2网络爬虫的主要类型2 1.1.3简单网络爬虫的架构3 1.1.4网络爬虫的应用场景3 1.2Python网络爬虫技术概况4 ...

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

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

  7. python windows 安装scrapy_Python Scrapy在windows上的安装方法

    如果想要学习爬虫,肯定会了解Scrapy,但安装Scrapy的方法相对于安装其他第三方库麻烦一点. 下面总结一下在我的电脑上安装Scrapy的方法,我的电脑是Windows10,32位操作系统.有如下 ...

  8. mac python3.8怎样安装scrapy_Python爬虫与mac下Scrapy配置

    用Python开发爬虫很方便. 本质:抓取---分析---存储 要点: (1)get请求:最基本抓取.用到 urllib  urllib2  requests  httplib2 . (2)表单登陆: ...

  9. python怎么安装各种模块_Python2.7安装和常用模块安装

    a.傻瓜安装下一步就行 b.把python.exe所在目录就是安装python路径和Scripts文件夹路径即F:\Python\Scripts添加到环境变量中 使用cmd命令easy_install ...

  10. python3.6安装scrapy-win7安装python3.6.1及scrapy

    ---恢复内容开始--- 第一篇博客,记录自己自学python的过程及问题. 首先下载python3.6.1及所需资料 百度云:https://pan.baidu.com/s/1geOEp6z 密码: ...

最新文章

  1. 寻找孪生素数(当p为素数时,p+2也为素数)
  2. PHP 语言结构(Language constructs)和函数的区别
  3. bio和bieos哪个标注模式好_阿里巴巴和亚马逊电商模式差异?哪个电商好做
  4. 子元素是字典列表转成字典
  5. 程序员圣诞节相册源码_程序员分享圣诞刷屏源码,这次朋友圈千万不要再@微信官方了!...
  6. python eel 多线程_Python 基础
  7. jquery ajax下拉联动,基于jQuery Ajax实现下拉框无刷新联动
  8. 汉堡菜单_开发人员在编写汉堡菜单时犯的错误
  9. 一周时间摸清一个行业
  10. Nginx正、反向代理以及负载均衡介绍
  11. 数值分析(6)-函数逼近的基本概念
  12. Cookie 和 Session 规则
  13. 用vb写计算机程序代码,用VB编写的抽奖程序源代码随机抽取不重复
  14. nodejs + echarts 图表展示
  15. DWM安装及简略配置教程
  16. 《四》大话 TypeScript 泛型
  17. 【懒懒的Tensorflow学习笔记一之快速入门】
  18. 深度学习进阶课程16---用ReL解决Vanishing Gradient问题
  19. Win10 更改桌面背景为图片不生效,马上自动变为纯色黑色
  20. C基础 | 【05】(内存结构以及复合类型)

热门文章

  1. 数电技术基础大恶补04:CMOS门电路
  2. SpringBoot 整合 Editormd(完整版)
  3. 支付宝沙箱集成无法唤起客户端
  4. 机器学习:弱监督学习简介
  5. linux ftp切换目录命令,linux FTP命令详解
  6. ubuntu创建“新建文本文档”的快捷方式
  7. 经纬度坐标转换xy坐标 python_Python 高斯坐标转经纬度算法
  8. 计算机软件需求说明编制指南gb/t 9385-2008,GBT 9385-2008 计算机软件需求说明编制指南.pdf...
  9. OBS录屏软件使用指南
  10. 腾讯笔试题:纸牌游戏