scrapy简单爬虫(一)

在terminal中

1.To create scrapy project : scrapy startproject ITcast

2. cd the path of  folder  spider , and input the command scrapy genspider itcast " itcast.cn"

(1)name = 'itcast': 爬虫识别名称,必须唯一
(2)allowed_domains = ['itcast.cn'] :  
搜索的域名范围,爬虫的约束区域,爬虫只爬取这个域名下的网页,不存在的URL会被呼略
(3)start_urls = ['http://itcast.cn/']: 
爬取的URL元祖列表,爬虫从这里开始抓取数据,所以,第一次下载的数据将会从这些urls开始,
其他子url将会从这些起始URL中继承性生成

    (4)def parse(self, response):解析的方法,每个初始url完成下载后将被调用,调用的时候传入每一个URL传回的Response对象来作为唯一参数,主要作用如下:

           负责解析返回的网页数据(response.body),提取结构化数据(生成item)

            生成需要下一页的URL请求

当python版本为python2.x时候,需要添加

import sys

reload(sys)

sys.setdefaultencoding("utf-8")


猜你喜欢

转载自blog.csdn.net/lx5090110/article/details/80703622