环境:Windows 7 x64 Python3.7.1 pycharm
1.1linux系统使用:pip install scrapy
1.2Windows系统:
1、新建一个项目,选择Python即可。我这里创建的项目名是demo。创建好后是一个空的项目。
2、点击pycharm下面的terminal,如下图所示:
在终端中输入:scrapy startproject demo 命令,创建scrapy项目,创建成功后会出现如下目录结构:
各文件作用大致如下:
3.1在终端中输入:cd demo(我这里输入demo是因为我的项目名是demo)
3.2在终端中输入:scrapy genspider books books.toscrape.com (scrapy genspider 应用名称 爬取网页的起始url)
5.1分析http://books.toscrape.com/页面。
由上图我们可以知道所有书籍都存放在div/ol/下的li标签中。这里我们只打印书名,由此我们可以像下面这样写来提取数据。
5.2books中的部分代码如下:
def parse(self, response):
'''
数据解析,提取。
:param response: 爬取到的response对象
:return:
'''
book_list = response.xpath('/html/body/div/div/div/div/section/div[2]/ol/li')
for book in book_list:
print(book.xpath('./article/div[1]/a/img/@alt').extract())
5.3在setting.py中配置如下:
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0' # UA头
ROBOTSTXT_OBEY = False # 如果为True表示准信robots协议,则大多数数据都爬不了。所以这里设置为Flase
LOG_LEVEL = 'ERROR' # 日志等级
5.4在终端中执行爬取命令:scrapy crawl books
# 打印内容如下
['A Light in the Attic']
['Tipping the Velvet']
['Soumission']
['Sharp Objects']
['Sapiens: A Brief History of Humankind']
['The Requiem Red']
['The Dirty Little Secrets of Getting Your Dream Job']
['The Coming Woman: A Novel Based on the Life of the Infamous Feminist, Victoria Woodhull']
['The Boys in the Boat: Nine Americans and Their Epic Quest for Gold at the 1936 Berlin Olympics']
['The Black Maria']
['Starving Hearts (Triangular Trade Trilogy, #1)']
["Shakespeare's Sonnets"]
['Set Me Free']
["Scott Pilgrim's Precious Little Life (Scott Pilgrim #1)"]
['Rip it Up and Start Again']
['Our Band Could Be Your Life: Scenes from the American Indie Underground, 1981-1991']
['Olio']
['Mesaerion: The Best Science Fiction Stories 1800-1849']
['Libertarianism for Beginners']
["It's Only the Himalayas"]
由此我们可以看出这里只是爬取了1页,下面来爬取所有书籍名称。
最终books.py的内容看起来像下面这样:
# -*- coding: utf-8 -*-
import scrapy
class BooksSpider(scrapy.Spider):
name = 'books' # 爬虫的唯一标识
allowed_domains = ['books.toscrape.com']
# 要爬取的起点,可以是多个。
start_urls = ['http://books.toscrape.com/']
url = 'http://books.toscrape.com/catalogue/page-%d.html' # url模板用于拼接新的url
page_num = 2
def parse(self, response):
'''
数据解析,提取。
:param response: 爬取到的response对象
:return:
'''
print(f'当前页数{self.page_num}') # 打印当前页数的数据
book_list = response.xpath('/html/body/div/div/div/div/section/div[2]/ol/li')
for book in book_list:
print(book.xpath('./article/div[1]/a/img/@alt').extract())
if self.page_num < 50: # 总共50页的内容
new_url = format(self.url % self.page_num) # 拼接处新的URL
self.page_num += 1 # 页数加1
yield scrapy.Request(url=new_url, callback=self.parse) # 手动发送请求
在终端中执行命令获取书名:scrapy crawl books
如果一切顺利你会看到打印的最终部分结果如下:
创建scrapy项目:scrapy startproject 爬虫项目名称。
创建爬虫应用:scrapy genspider books books.toscrape.com ((scrapy genspider 应用名称 爬取网页的起始url))应用名称在整个项目中作为唯一标识,不能出现同名的爬虫应用。
运行爬虫程序:scrapy crawl books(scrapy crawl 爬虫应用)。
parse方法:当一个页面下载完成后,Scrapy引擎会回调一个我们指定的页面解析函数(默认为parse方法)解析页面。一个页面解析函数通常需要完成以下两个任务:
1、提取页面中的数据(使用XPath或CSS选择器)。
2、提取页面中的链接,并产生对链接页面的下载请求。
页面解析函数通常被实现成一个生成器函数,每一项从页面中提取的数据以及每一个对链接页面的下载请求都由yield语句提交给Scrapy引擎。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章