scrapy框架安装
笔者为windows系统。scrapy框架可以使用pip下载,但在下载安装过程中可能会因为缺少c++ 14.0的环境导致scrapy无法安装。
解决方案
在http://fstab.net/pypi/simple/twisted/中找到对应的包进行下载,如笔者为python3.6的环境则下载
Twisted-17.1.0-cp36-cp36m-win_amd64.whl
使用pip安装这个包
pip install [包所在的路径]Twisted-17.1.0-cp36-cp36m-win_amd64.whl
然后再执行pip install scrapy即可安装成功
scrapy框架初尝试
建立scrapy项目
scrapy startproject mySpider
这条命令会建立一个文件夹,目录树如下
mySpider/
scrapy.cfg
mySpider/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
在spiders文件夹中创建py文件写入如下代码
import scrapy
class TestSpider(scrapy.Spider):
name = "test" # 这个爬虫的识别名称,必须是唯一的,在不同的爬虫必须定义不同的名字。
start_urls = (
'http://www.baidu.com/',
)# 起始爬取的网页
def parse(self, response):
print(response.body)
随后可在控制台内运行该爬虫
scrapy crawl test
即可在控制台内看到输出的爬虫内容
笔者目前尚未深入了解scrapy框架,如有错误之处,请见谅并指出。