scrapy 爬虫框架

scrapy startproject <project_name> 创建新项目

<project_name>/<project_name>/spiders 目录下创建quotes_spider.py 文件。
start_url 里面写入要爬取的网址。
def parse(self, response) 里面写默认回调函数。

在<project_name> 目录下执行 scrapy crawl quotes 来开启爬行。

scrapy shell <url> 启动 shell

response.css('title') 定位到标题
response.css('title::text').getall() 获取文本 get() 只取第一个
response.xpath('//title/text()').get() 获取文本

scrapy crawl quotes -o quotes.json 保存为quotes.json 文件
response.css('li.next a:attr(href)').get() 获取href属性。

posted @ 2020-05-25 17:51  那个白熊  阅读(133)  评论(0编辑  收藏  举报