SlideShare uma empresa Scribd logo
1 de 17
h4353062@hotmail.com
鍾怡傑
 甚麼是 Scrapy ?
 如何安裝 ?
 Go! Go! 開始吧~
 實作第一個 Spider
 Demo
 Scrapy 是一個 Python 的框架,可以簡單用
Python 的程式碼寫網路蜘蛛,很方便的抓網頁上
的資料。
 Scrapy at a glance
 Frequently Asked Questions
 需要安裝
◦ Python 2.7
◦ Lxml
◦ OpenSSL
◦ pip or easy_install
 安裝 Scrapy
◦ pip install Scrapy
◦ or
◦ easy_install Scrapy
http://doc.scrapy.org/en/latest/intro/install.html
 Step
1. 編輯 /etc/apt/sources.list 加入這行
deb http://archive.scrapy.org/ubuntu precise main
2. 執行 curl –s
http://archive.scrapy.org/ubuntu/archive.key | sudo apt-key add –
3. 升級 sudo apt-get update
4. 安裝 sudo apt-get install scrapy-<Version>
5. 測試執行 scrapy 指令
http://doc.scrapy.org/en/latest/topics/ubuntu.html
http://doc.scrapy.org/en/latest/topics/architecture.html
 甚麼是 Scrapy Shell ?
◦ 一個 python 的直譯器,可以動態顯示執行的結果。
 執行 scrapy shell “<URL>”
 Ex:
scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
 讓我們試一下吧~
 觀察輸出的結果
In [1]: sel.xpath('//title')
In [2]: sel.xpath('//title').extract()
In [3]: sel.xpath('//title/text()')
In [4]: sel.xpath('//title/text()').extract()
In [5]: sel.xpath('//title/text()').re('(w+):')
 由輸出結果可知,Scrapy 使用 Xpath 語法抓網頁的資
訊,預設輸出使用 JSON 格式
 對於 Xpath 語法不熟的話,可參考以下網址:
1. http://www.w3schools.com/XPath/
2. http://msdn.microsoft.com/zh-
tw/library/ms256086(v=vs.110).aspx
 練習資源
1. Xpath Tester
2. Chrome 瀏覽器的 Xpath Helper 套件
Scrapy Tutorial
 基本流程:
1. 建立一個新 Scrapy 專案
2. 定義你需要的 Item
3. 寫一個 Spider 抓資料
4. 寫一個 Item Pipeline 存抓到的資料
 scrapy startproject <Project_Name>
scrapy startproject tutorial
tutorial/
├── tutorial
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
│ └── __init__.py
└── scrapy.cfg
 編輯 tutorial/tutorial/items.py
from scrapy.item import Item, Field
class DmozItem(Item):
title = Field()
link = Field()
desc = Field()
 編輯 tutorial/tutorial/spiders/dmoz_spider.py
from scrapy.spider import Spider
from scrapy.selector import Selector
from tutorial.items import DmozItem
class DmozSpider(Spider):
name = “dmoz”
allowed_domains = ["dmoz.org"]
start_urls = [
“http://www.dmoz.org/Computers/Programming/Languages/Python/B
"http://www.dmoz.org/Computers/Programming/Languages/Python/R
]
def parse(self, response):
sel = Selector(response)
sites = sel.xpath('//ul/li')
items = []
for site in sites:
item = DmozItem() #之前 Step2 定義的 Item
item['title'] = site.xpath('a/text()').extract()
item['link'] = site.xpath('a/@href').extract()
item['desc'] = site.xpath('text()').extract()
items.append(item)
return items
 執行路徑必須在 tutorial/tutorial/spiders/
 抓到的資料儲存在 spiders/items.json
scrapy crawl dmoz -o items.json -t json
參考資料: Scrapy 0.22 documentation

Mais conteúdo relacionado

Mais procurados

Koa 正在等一個人
Koa 正在等一個人Koa 正在等一個人
Koa 正在等一個人Fred Chien
 
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2Yang FaFa
 
Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)Chu-Siang Lai
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例Will Huang
 
Communication with python_http_module
Communication with python_http_moduleCommunication with python_http_module
Communication with python_http_moduledreampuf
 
twMVC#43 C#10 新功能介紹
twMVC#43 C#10 新功能介紹twMVC#43 C#10 新功能介紹
twMVC#43 C#10 新功能介紹twMVC
 
Django入门
Django入门Django入门
Django入门oikomi
 
浮云脱水小说站的搭建
浮云脱水小说站的搭建浮云脱水小说站的搭建
浮云脱水小说站的搭建jondynet
 
WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」Orange Tsai
 
Java_07:Thread
Java_07:ThreadJava_07:Thread
Java_07:ThreadBrad Chao
 
Node.js從無到有 基本課程
Node.js從無到有 基本課程Node.js從無到有 基本課程
Node.js從無到有 基本課程Simon Su
 
Web scraping入門1
Web scraping入門1Web scraping入門1
Web scraping入門1兆欽 丘
 
twMVC#43 YARP
twMVC#43 YARPtwMVC#43 YARP
twMVC#43 YARPtwMVC
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門Will Huang
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管Will Huang
 
Tornado开发实践
Tornado开发实践Tornado开发实践
Tornado开发实践ZY Zhang
 
Nginx+常见应用技术指南
Nginx+常见应用技术指南Nginx+常见应用技术指南
Nginx+常见应用技术指南andy54321
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)Will Huang
 
课题二:Node.js那些事儿
课题二:Node.js那些事儿课题二:Node.js那些事儿
课题二:Node.js那些事儿Liu Allen
 

Mais procurados (20)

Koa 正在等一個人
Koa 正在等一個人Koa 正在等一個人
Koa 正在等一個人
 
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
[Talk] 2013 06-02-hadoop整合應用-crawlzilla@hadoop q2
 
Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)Automate with Ansible basic (2/e)
Automate with Ansible basic (2/e)
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
 
Communication with python_http_module
Communication with python_http_moduleCommunication with python_http_module
Communication with python_http_module
 
twMVC#43 C#10 新功能介紹
twMVC#43 C#10 新功能介紹twMVC#43 C#10 新功能介紹
twMVC#43 C#10 新功能介紹
 
Django入门
Django入门Django入门
Django入门
 
浮云脱水小说站的搭建
浮云脱水小说站的搭建浮云脱水小说站的搭建
浮云脱水小说站的搭建
 
WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」WebConf 2013「Best Practices - The Upload」
WebConf 2013「Best Practices - The Upload」
 
Java_07:Thread
Java_07:ThreadJava_07:Thread
Java_07:Thread
 
Node.js從無到有 基本課程
Node.js從無到有 基本課程Node.js從無到有 基本課程
Node.js從無到有 基本課程
 
Web scraping入門1
Web scraping入門1Web scraping入門1
Web scraping入門1
 
twMVC#43 YARP
twMVC#43 YARPtwMVC#43 YARP
twMVC#43 YARP
 
Banquet 15
Banquet 15Banquet 15
Banquet 15
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管
 
Tornado开发实践
Tornado开发实践Tornado开发实践
Tornado开发实践
 
Nginx+常见应用技术指南
Nginx+常见应用技术指南Nginx+常见应用技术指南
Nginx+常见应用技术指南
 
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
使用 C#/Razor 開發互動式 WebAssembly 網站 (Modern Web 2018)
 
课题二:Node.js那些事儿
课题二:Node.js那些事儿课题二:Node.js那些事儿
课题二:Node.js那些事儿
 

Destaque

Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2kriszyp
 
Big data at scrapinghub
Big data at scrapinghubBig data at scrapinghub
Big data at scrapinghubDana Brophy
 
XPath for web scraping
XPath for web scrapingXPath for web scraping
XPath for web scrapingScrapinghub
 
Downloading the internet with Python + Scrapy
Downloading the internet with Python + ScrapyDownloading the internet with Python + Scrapy
Downloading the internet with Python + ScrapyErin Shellman
 
How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...
How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...
How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...Anton
 
Python, web scraping and content management: Scrapy and Django
Python, web scraping and content management: Scrapy and DjangoPython, web scraping and content management: Scrapy and Django
Python, web scraping and content management: Scrapy and DjangoSammy Fung
 
電腦不只會幫你選土豆,還會幫你選新聞
電腦不只會幫你選土豆,還會幫你選新聞電腦不只會幫你選土豆,還會幫你選新聞
電腦不只會幫你選土豆,還會幫你選新聞Andy Dai
 
均一Gae甘苦談
均一Gae甘苦談均一Gae甘苦談
均一Gae甘苦談蘇 倚恩
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsSolution4Future
 
Web backends development using Python
Web backends development using PythonWeb backends development using Python
Web backends development using PythonAyun Park
 
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014Toomore
 
Learn REST API with Python
Learn REST API with PythonLearn REST API with Python
Learn REST API with PythonLarry Cai
 
Visualising Data with Code
Visualising Data with CodeVisualising Data with Code
Visualising Data with CodeRi Liu
 
Web Scraping in Python with Scrapy
Web Scraping in Python with ScrapyWeb Scraping in Python with Scrapy
Web Scraping in Python with Scrapyorangain
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBDeveloping RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBNicola Iarocci
 

Destaque (18)

Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2Java Script Based Client Server Webapps 2
Java Script Based Client Server Webapps 2
 
Big data at scrapinghub
Big data at scrapinghubBig data at scrapinghub
Big data at scrapinghub
 
Scrapy.for.dummies
Scrapy.for.dummiesScrapy.for.dummies
Scrapy.for.dummies
 
Scrapy
ScrapyScrapy
Scrapy
 
XPath for web scraping
XPath for web scrapingXPath for web scraping
XPath for web scraping
 
Downloading the internet with Python + Scrapy
Downloading the internet with Python + ScrapyDownloading the internet with Python + Scrapy
Downloading the internet with Python + Scrapy
 
How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...
How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...
How to Scrap Any Website's content using ScrapyTutorial of How to scrape (cra...
 
Mining legal texts with Python
Mining legal texts with PythonMining legal texts with Python
Mining legal texts with Python
 
Python, web scraping and content management: Scrapy and Django
Python, web scraping and content management: Scrapy and DjangoPython, web scraping and content management: Scrapy and Django
Python, web scraping and content management: Scrapy and Django
 
電腦不只會幫你選土豆,還會幫你選新聞
電腦不只會幫你選土豆,還會幫你選新聞電腦不只會幫你選土豆,還會幫你選新聞
電腦不只會幫你選土豆,還會幫你選新聞
 
均一Gae甘苦談
均一Gae甘苦談均一Gae甘苦談
均一Gae甘苦談
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
 
Web backends development using Python
Web backends development using PythonWeb backends development using Python
Web backends development using Python
 
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
如何用 grs 擷取台灣上市股票股價資訊 PyCon APAC 2014
 
Learn REST API with Python
Learn REST API with PythonLearn REST API with Python
Learn REST API with Python
 
Visualising Data with Code
Visualising Data with CodeVisualising Data with Code
Visualising Data with Code
 
Web Scraping in Python with Scrapy
Web Scraping in Python with ScrapyWeb Scraping in Python with Scrapy
Web Scraping in Python with Scrapy
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBDeveloping RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDB
 

Semelhante a 快快樂樂學 Scrapy

Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuMarsZ Chen
 
如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)
如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)
如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)Yanwei Liu
 
Linux network monitoring hands-on pratice
Linux network monitoring hands-on praticeLinux network monitoring hands-on pratice
Linux network monitoring hands-on praticeKenny (netman)
 
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...Laird Cheng
 
前端工程師一定要知道的 Docker 虛擬化容器技巧
前端工程師一定要知道的 Docker 虛擬化容器技巧前端工程師一定要知道的 Docker 虛擬化容器技巧
前端工程師一定要知道的 Docker 虛擬化容器技巧Chu-Siang Lai
 
02.python.开发最佳实践
02.python.开发最佳实践02.python.开发最佳实践
02.python.开发最佳实践Na Lee
 
使用Supervisor守护python进程
使用Supervisor守护python进程使用Supervisor守护python进程
使用Supervisor守护python进程果 果
 
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1GNU Autoconf / Automake #1
GNU Autoconf / Automake #1imacat .
 
Android系统移植技术详解
Android系统移植技术详解Android系统移植技术详解
Android系统移植技术详解zzc89522
 
Python的module机制与最佳实践
Python的module机制与最佳实践Python的module机制与最佳实践
Python的module机制与最佳实践Leo Zhou
 
Puppet安装总结
Puppet安装总结Puppet安装总结
Puppet安装总结Yiwei Ma
 
Ruby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for MacRuby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for MacMarsZ Chen
 
Docker 基礎介紹與實戰
Docker 基礎介紹與實戰Docker 基礎介紹與實戰
Docker 基礎介紹與實戰Bo-Yi Wu
 
141118 Raspberry Pi 電鈴工作坊@松山文創園區
141118 Raspberry Pi 電鈴工作坊@松山文創園區141118 Raspberry Pi 電鈴工作坊@松山文創園區
141118 Raspberry Pi 電鈴工作坊@松山文創園區CAVEDU Education
 
Python包管理工具介绍
Python包管理工具介绍Python包管理工具介绍
Python包管理工具介绍Young King
 
Graphlab Create使用說明,以SFrame為例
Graphlab Create使用說明,以SFrame為例Graphlab Create使用說明,以SFrame為例
Graphlab Create使用說明,以SFrame為例Simon Li
 

Semelhante a 快快樂樂學 Scrapy (20)

Ruby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for UbuntuRuby on Rails 開發環境建置 for Ubuntu
Ruby on Rails 開發環境建置 for Ubuntu
 
如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)
如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)
如何幫樹莓派安裝常用的Python套件(How to Install Python Package on Raspberry Pi)
 
Git安裝
Git安裝Git安裝
Git安裝
 
Linux network monitoring hands-on pratice
Linux network monitoring hands-on praticeLinux network monitoring hands-on pratice
Linux network monitoring hands-on pratice
 
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
使用 Load Balancer 與 Redis 部署 LAMP Server 高併發架構 - Global Azure Taiwan 20200425 ...
 
前端工程師一定要知道的 Docker 虛擬化容器技巧
前端工程師一定要知道的 Docker 虛擬化容器技巧前端工程師一定要知道的 Docker 虛擬化容器技巧
前端工程師一定要知道的 Docker 虛擬化容器技巧
 
02.python.开发最佳实践
02.python.开发最佳实践02.python.开发最佳实践
02.python.开发最佳实践
 
使用Supervisor守护python进程
使用Supervisor守护python进程使用Supervisor守护python进程
使用Supervisor守护python进程
 
Linuxguide4f2e
Linuxguide4f2eLinuxguide4f2e
Linuxguide4f2e
 
LinuxGuide4F2E
LinuxGuide4F2ELinuxGuide4F2E
LinuxGuide4F2E
 
GNU Autoconf / Automake #1
GNU Autoconf / Automake #1GNU Autoconf / Automake #1
GNU Autoconf / Automake #1
 
Android系统移植技术详解
Android系统移植技术详解Android系统移植技术详解
Android系统移植技术详解
 
Python的module机制与最佳实践
Python的module机制与最佳实践Python的module机制与最佳实践
Python的module机制与最佳实践
 
Puppet安装总结
Puppet安装总结Puppet安装总结
Puppet安装总结
 
Ruby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for MacRuby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for Mac
 
Docker 基礎介紹與實戰
Docker 基礎介紹與實戰Docker 基礎介紹與實戰
Docker 基礎介紹與實戰
 
141118 Raspberry Pi 電鈴工作坊@松山文創園區
141118 Raspberry Pi 電鈴工作坊@松山文創園區141118 Raspberry Pi 電鈴工作坊@松山文創園區
141118 Raspberry Pi 電鈴工作坊@松山文創園區
 
Pipi pi
Pipi piPipi pi
Pipi pi
 
Python包管理工具介绍
Python包管理工具介绍Python包管理工具介绍
Python包管理工具介绍
 
Graphlab Create使用說明,以SFrame為例
Graphlab Create使用說明,以SFrame為例Graphlab Create使用說明,以SFrame為例
Graphlab Create使用說明,以SFrame為例
 

快快樂樂學 Scrapy

  • 2.  甚麼是 Scrapy ?  如何安裝 ?  Go! Go! 開始吧~  實作第一個 Spider  Demo
  • 3.  Scrapy 是一個 Python 的框架,可以簡單用 Python 的程式碼寫網路蜘蛛,很方便的抓網頁上 的資料。  Scrapy at a glance  Frequently Asked Questions
  • 4.  需要安裝 ◦ Python 2.7 ◦ Lxml ◦ OpenSSL ◦ pip or easy_install  安裝 Scrapy ◦ pip install Scrapy ◦ or ◦ easy_install Scrapy http://doc.scrapy.org/en/latest/intro/install.html
  • 5.  Step 1. 編輯 /etc/apt/sources.list 加入這行 deb http://archive.scrapy.org/ubuntu precise main 2. 執行 curl –s http://archive.scrapy.org/ubuntu/archive.key | sudo apt-key add – 3. 升級 sudo apt-get update 4. 安裝 sudo apt-get install scrapy-<Version> 5. 測試執行 scrapy 指令 http://doc.scrapy.org/en/latest/topics/ubuntu.html
  • 6.
  • 8.  甚麼是 Scrapy Shell ? ◦ 一個 python 的直譯器,可以動態顯示執行的結果。  執行 scrapy shell “<URL>”  Ex: scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
  • 9.  讓我們試一下吧~  觀察輸出的結果 In [1]: sel.xpath('//title') In [2]: sel.xpath('//title').extract() In [3]: sel.xpath('//title/text()') In [4]: sel.xpath('//title/text()').extract() In [5]: sel.xpath('//title/text()').re('(w+):')
  • 10.  由輸出結果可知,Scrapy 使用 Xpath 語法抓網頁的資 訊,預設輸出使用 JSON 格式  對於 Xpath 語法不熟的話,可參考以下網址: 1. http://www.w3schools.com/XPath/ 2. http://msdn.microsoft.com/zh- tw/library/ms256086(v=vs.110).aspx  練習資源 1. Xpath Tester 2. Chrome 瀏覽器的 Xpath Helper 套件
  • 12.  基本流程: 1. 建立一個新 Scrapy 專案 2. 定義你需要的 Item 3. 寫一個 Spider 抓資料 4. 寫一個 Item Pipeline 存抓到的資料
  • 13.  scrapy startproject <Project_Name> scrapy startproject tutorial tutorial/ ├── tutorial │ ├── __init__.py │ ├── items.py │ ├── pipelines.py │ ├── settings.py │ └── spiders │ └── __init__.py └── scrapy.cfg
  • 14.  編輯 tutorial/tutorial/items.py from scrapy.item import Item, Field class DmozItem(Item): title = Field() link = Field() desc = Field()
  • 15.  編輯 tutorial/tutorial/spiders/dmoz_spider.py from scrapy.spider import Spider from scrapy.selector import Selector from tutorial.items import DmozItem class DmozSpider(Spider): name = “dmoz” allowed_domains = ["dmoz.org"] start_urls = [ “http://www.dmoz.org/Computers/Programming/Languages/Python/B "http://www.dmoz.org/Computers/Programming/Languages/Python/R ] def parse(self, response): sel = Selector(response) sites = sel.xpath('//ul/li') items = [] for site in sites: item = DmozItem() #之前 Step2 定義的 Item item['title'] = site.xpath('a/text()').extract() item['link'] = site.xpath('a/@href').extract() item['desc'] = site.xpath('text()').extract() items.append(item) return items
  • 16.  執行路徑必須在 tutorial/tutorial/spiders/  抓到的資料儲存在 spiders/items.json scrapy crawl dmoz -o items.json -t json
  • 17. 參考資料: Scrapy 0.22 documentation