在Linux系統(tǒng)上安裝Python的Scrapy框架的教程
這是一款提取網(wǎng)站數(shù)據(jù)的開源工具。Scrapy框架用Python開發(fā)而成,它使抓取工作又快又簡單,且可擴展。我們已經(jīng)在virtual box中創(chuàng)建一臺虛擬機(VM)并且在上面安裝了Ubuntu 14.04 LTS。
安裝 Scrapy
Scrapy依賴于Python、開發(fā)庫和pip。Python最新的版本已經(jīng)在Ubuntu上預裝了。因此我們在安裝Scrapy之前只需安裝pip和python開發(fā)庫就可以了。
pip是作為python包索引器easy_install的替代品,用于安裝和管理Python包。pip包的安裝可見圖 1。
sudo apt-get install python-pip

圖:1 pip安裝
我們必須要用下面的命令安裝python開發(fā)庫。如果包沒有安裝那么就會在安裝scrapy框架的時候報關于python.h頭文件的錯誤。
sudo apt-get install python-dev

圖:2 Python 開發(fā)庫
scrapy框架既可從deb包安裝也可以從源碼安裝。在圖3中我們用pip(Python 包管理器)安裝了deb包了。
sudo pip install scrapy

圖:3 Scrapy 安裝
圖4中scrapy的成功安裝需要一些時間。

圖:4 成功安裝Scrapy框架
使用scrapy框架提取數(shù)據(jù)
基礎教程
我們將用scrapy從fatwallet.com上提取商店名稱(賣卡的店)。首先,我們使用下面的命令新建一個scrapy項目“store name”, 見圖5。
$sudo scrapy startproject store_name

圖:5 Scrapy框架新建項目
上面的命令在當前路徑創(chuàng)建了一個“store_name”的目錄。項目主目錄下包含的文件/文件夾見圖6。
$sudo ls –lR store_name

圖:6 store_name項目的內容
每個文件/文件夾的概要如下:
- scrapy.cfg 是項目配置文件
- store_name/ 主目錄下的另一個文件夾。 這個目錄包含了項目的python代碼
- store_name/items.py 包含了將由蜘蛛爬取的項目
- store_name/pipelines.py 是管道文件
- store_name/settings.py 是項目的配置文件
- store_name/spiders/, 包含了用于爬取的蜘蛛
由于我們要從fatwallet.com上如提取店名,因此我們如下修改文件(LCTT 譯注:這里沒說明是哪個文件,譯者認為應該是 items.py)。
import scrapy
class StoreNameItem(scrapy.Item):
name = scrapy.Field() # 取出卡片商店的名稱
之后我們要在項目的store_name/spiders/文件夾下寫一個新的蜘蛛。蜘蛛是一個python類,它包含了下面幾個必須的屬性:
- 蜘蛛名 (name )
- 爬取起點url (start_urls)
- 包含了從響應中提取需要內容相應的正則表達式的解析方法。解析方法對爬蟲而言很重要。
我們在storename/spiders/目錄下創(chuàng)建了“storename.py”爬蟲,并添加如下的代碼來從fatwallet.com上提取店名。爬蟲的輸出寫到文件(StoreName.txt)中,見圖7。
from scrapy.selector import Selector
from scrapy.spider import BaseSpider
from scrapy.http import Request
from scrapy.http import FormRequest
import re
class StoreNameItem(BaseSpider):
name = "storename"
allowed_domains = ["fatwallet.com"]
start_urls = ["http://fatwallet.com/cash-back-shopping/"]
def parse(self,response):
output = open('StoreName.txt','w')
resp = Selector(response)
tags = resp.xpath('//tr[@class="storeListRow"]|\
//tr[@class="storeListRow even"]|\
//tr[@class="storeListRow even last"]|\
//tr[@class="storeListRow last"]').extract()
for i in tags:
i = i.encode('utf-8', 'ignore').strip()
store_name = ''
if re.search(r"class=\"storeListStoreName\">.*?<",i,re.I|re.S):
store_name = re.search(r"class=\"storeListStoreName\">.*?<",i,re.I|re.S).group()
store_name = re.search(r">.*?<",store_name,re.I|re.S).group()
store_name = re.sub(r'>',"",re.sub(r'<',"",store_name,re.I))
store_name = re.sub(r'&',"&",re.sub(r'&',"&",store_name,re.I))
#print store_name
output.write(store_name+""+"\n")

- 在python3.9下如何安裝scrapy的方法
- Python3環(huán)境安裝Scrapy爬蟲框架過程及常見錯誤
- 圖文詳解python安裝Scrapy框架步驟
- 詳解Python網(wǎng)絡框架Django和Scrapy安裝指南
- 詳解Python安裝scrapy的正確姿勢
- mac下給python3安裝requests庫和scrapy庫的實例
- Python之Scrapy爬蟲框架安裝及簡單使用詳解
- Python2.7下安裝Scrapy框架步驟教程
- Python3安裝Scrapy的方法步驟
- python安裝Scrapy圖文教程
- python中安裝Scrapy模塊依賴包匯總
- windows10系統(tǒng)中安裝python3.x+scrapy教程
- Python安裝Scrapy庫的常見報錯解決
相關文章
關于matplotlib-legend 位置屬性 loc 使用說明
這篇文章主要介紹了關于matplotlib-legend 位置屬性 loc 使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python 找出list中最大或者最小幾個數(shù)的索引方法
今天小編就為大家分享一篇python 找出list中最大或者最小幾個數(shù)的索引方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python Django view 兩種return的實現(xiàn)方式
這篇文章主要介紹了Python Django view 兩種return的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python中用format函數(shù)格式化字符串的用法
這篇文章主要介紹了Python中用format函數(shù)格式化字符串的用法,格式化字符串是Python學習當中的基礎知識,本文主要針對Python2.7.x版本,需要的朋友可以參考下2015-04-04

