国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
學(xué)會(huì)Python這幾個(gè)類庫使用,快速寫爬蟲不是問題(詳細(xì)步驟附源碼)

作為一種便捷地收集網(wǎng)上信息并從中抽取出可用信息的方式,網(wǎng)絡(luò)爬蟲技術(shù)變得越來越有用。使用Python這樣的簡(jiǎn)單編程語言,你可以使用少量編程技能就可以爬取復(fù)雜的網(wǎng)站。


如果手機(jī)上顯示代碼錯(cuò)亂,請(qǐng)分享到QQ或者其他地方,用電腦查看?。?!


python能干的東西有很多,這里不再過多敘述,直接重點(diǎn)干貨。


首先對(duì)的scrapy命令行使用的一個(gè)介紹。

創(chuàng)建爬蟲項(xiàng)目

scrapy startproject 項(xiàng)目名

例子如下:

localhost:spider zhaofan$ scrapy startproject test1New Scrapy project 'test1', using template directory '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scrapy/templates/project', created in: /Users/zhaofan/Documents/python_project/spider/test1You can start your first spider with: cd test1 scrapy genspider example example.comlocalhost:spider zhaofan$

這個(gè)時(shí)候爬蟲的目錄結(jié)構(gòu)就已經(jīng)創(chuàng)建完成了,目錄結(jié)構(gòu)如下:

|____scrapy.cfg|____test1| |______init__.py| |____items.py| |____middlewares.py| |____pipelines.py| |____settings.py| |____spiders| | |______init__.py

接著我們按照提示可以生成一個(gè)spider,這里以百度作為例子,生成spider的命令格式為;

scrapy genspider 爬蟲名字 爬蟲的網(wǎng)址

localhost:test1 zhaofan$ scrapy genspider baiduSpider baidu.comCreated spider 'baiduSpider' using template 'basic' in module: test1.spiders.baiduSpiderlocalhost:test1 zhaofan$

關(guān)于命令詳細(xì)使用

命令的使用范圍

這里的命令分為全局的命令和項(xiàng)目的命令,全局的命令表示可以在任何地方使用,而項(xiàng)目的命令只能在項(xiàng)目目錄下使用

全局的命令有:

startproject

genspider

settings

runspider

shell

fetch

view

version

項(xiàng)目命令有:

crawl

check

list

edit

parse

bench

startproject

這個(gè)命令沒什么過多的用法,就是在創(chuàng)建爬蟲項(xiàng)目的時(shí)候用

genspider

用于生成爬蟲,這里scrapy提供給我們不同的幾種模板生成spider,默認(rèn)用的是basic,我們可以通過命令查看所有的模板

localhost:test1 zhaofan$ scrapy genspider -lAvailable templates: basic crawl csvfeed xmlfeedlocalhost:test1 zhaofan$

當(dāng)我們創(chuàng)建的時(shí)候可以指定模板,不指定默認(rèn)用的basic,如果想要指定模板則通過

scrapy genspider -t 模板名字

localhost:test1 zhaofan$ scrapy genspider -t crawl zhihuspider zhihu.comCreated spider 'zhihuspider' using template 'crawl' in module: test1.spiders.zhihuspiderlocalhost:test1 zhaofan$

crawl

這個(gè)是用去啟動(dòng)spider爬蟲格式為:

scrapy crawl 爬蟲名字

這里需要注意這里的爬蟲名字和通過scrapy genspider 生成爬蟲的名字是一致的

check

用于檢查代碼是否有錯(cuò)誤,scrapy check

list

scrapy list列出所有可用的爬蟲

fetch

scrapy fetch url地址

該命令會(huì)通過scrapy downloader 講網(wǎng)頁的源代碼下載下來并顯示出來

這里有一些參數(shù):

--nolog 不打印日志

--headers 打印響應(yīng)頭信息

--no-redirect 不做跳轉(zhuǎn)

view

scrapy view url地址

該命令會(huì)講網(wǎng)頁document內(nèi)容下載下來,并且在瀏覽器顯示出來

?

因?yàn)楝F(xiàn)在很多網(wǎng)站的數(shù)據(jù)都是通過ajax請(qǐng)求來加載的,這個(gè)時(shí)候直接通過requests請(qǐng)求是無法獲取我們想要的數(shù)據(jù),所以這個(gè)view命令可以幫助我們很好的判斷

shell

這是一個(gè)命令行交互模式

通過scrapy shell url地址進(jìn)入交互模式

這里我么可以通過css選擇器以及xpath選擇器獲取我們想要的內(nèi)容(xpath以及css選擇的用法會(huì)在下個(gè)文章中詳細(xì)說明),例如我們通過scrapy shell http://www.baidu.com

?

這里最后給我們返回一個(gè)response,這里的response就和我們通requests請(qǐng)求網(wǎng)頁獲取的數(shù)據(jù)是相同的。

view(response)會(huì)直接在瀏覽器顯示結(jié)果

response.text 獲取網(wǎng)頁的文本

下圖是css選擇器的一個(gè)簡(jiǎn)單用法

?

settings

獲取當(dāng)前的配置信息

通過scrapy settings -h可以獲取這個(gè)命令的所有幫助信息

localhost:jobboleSpider zhaofan$ scrapy settings -hUsage===== scrapy settings [options]Get settings valuesOptions=======--help, -h show this help message and exit--get=SETTING print raw setting value--getbool=SETTING print setting value, interpreted as a boolean--getint=SETTING print setting value, interpreted as an integer--getfloat=SETTING print setting value, interpreted as a float--getlist=SETTING print setting value, interpreted as a listGlobal Options----------------logfile=FILE log file. if omitted stderr will be used--loglevel=LEVEL, -L LEVEL log level (default: DEBUG)--nolog disable logging completely--profile=FILE write python cProfile stats to FILE--pidfile=FILE write process ID to FILE--set=NAME=VALUE, -s NAME=VALUE set/override setting (may be repeated)--pdb enable pdb on failure

拿一個(gè)例子進(jìn)行簡(jiǎn)單的演示:(這里是我的這個(gè)項(xiàng)目的settings配置文件中配置了數(shù)據(jù)庫的相關(guān)信息,可以通過這種方式獲取,如果沒有獲取的則為None)

localhost:jobboleSpider zhaofan$ scrapy settings --get=MYSQL_HOST192.168.1.18localhost:jobboleSpider zhaofan$

runspider

這個(gè)和通過crawl啟動(dòng)爬蟲不同,這里是scrapy runspider 爬蟲文件名稱

所有的爬蟲文件都是在項(xiàng)目目錄下的spiders文件夾中

version

查看版本信息,并查看依賴庫的信息

localhost:~ zhaofan$ scrapy versionScrapy 1.3.2localhost:~ zhaofan$ scrapy version -vScrapy : 1.3.2lxml : 3.7.3.0libxml2 : 2.9.4cssselect : 1.0.1parsel : 1.1.0w3lib : 1.17.0Twisted : 17.1.0Python : 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) - [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]pyOpenSSL : 16.2.0 (OpenSSL 1.0.2k 26 Jan 2017)Platform : Darwin-16.6.0-x86_64-i386-64bit


Scrapy提取數(shù)據(jù)有自己的一套機(jī)制,被稱作選擇器(selectors),通過特定的Xpath或者CSS表達(dá)式來選擇HTML文件的某個(gè)部分

Xpath是專門在XML文件中選擇節(jié)點(diǎn)的語言,也可以用在HTML上。

CSS是一門將HTML文檔樣式化語言,選擇器由它定義,并與特定的HTML元素的樣式相關(guān)聯(lián)。

XPath選擇器

常用的路徑表達(dá)式,這里列舉了一些常用的,XPath的功能非常強(qiáng)大,內(nèi)含超過100個(gè)的內(nèi)建函數(shù)。

下面為常用的方法

nodeName 選取此節(jié)點(diǎn)的所有節(jié)點(diǎn)/ 從根節(jié)點(diǎn)選取// 從匹配選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn),不考慮它們的位置. 選擇當(dāng)前節(jié)點(diǎn).. 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)@ 選取屬性* 匹配任何元素節(jié)點(diǎn)@* 匹配任何屬性節(jié)點(diǎn)Node() 匹配任何類型的節(jié)點(diǎn)

CSS選擇器

CSS層疊樣式表,語法由兩個(gè)主要部分組成:選擇器,一條或多條聲明

Selector {declaration1;declaration2;……}

下面為常用的使用方法

.class .color 選擇class=”color”的所有元素#id #info 選擇id=”info”的所有元素* * 選擇所有元素element p 選擇所有的p元素element,element div,p 選擇所有div元素和所有p元素element element div p 選擇div標(biāo)簽內(nèi)部的所有p元素[attribute] [target] 選擇帶有targe屬性的所有元素[arrtibute=value] [target=_blank] 選擇target=”_blank”的所有元素

選擇器的使用例子

上面我們列舉了兩種選擇器的常用方法,下面通過scrapy幫助文檔提供的一個(gè)地址來做演示

地址:http://doc.scrapy.org/en/latest/_static/selectors-sample1.html

這個(gè)地址的網(wǎng)頁源碼為:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<base href='http://example.com/' />
<title>Example website</title>
</head>
<body>
<div id='images'>
<a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
<a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
<a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
<a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
<a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
</div>
</body>
</html>

我們通過scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html來演示兩種選擇器的功能

獲取title

這里的extract_first()就可以獲取title標(biāo)簽的文本內(nèi)容,因?yàn)槲覀兊谝粋€(gè)通過xpath返回的結(jié)果是一個(gè)列表,所以我們通過extract()之后返回的也是一個(gè)列表,而extract_first()可以直接返回第一個(gè)值,extract_first()有一個(gè)參數(shù)default,例如:extract_first(default="")表示如果匹配不到返回一個(gè)空

In [1]: response.xpath('//title/text()')Out[1]: [<Selector xpath='//title/text()' data='Example website'>]In [2]: response.xpath('//title/text()').extract_first()Out[2]: 'Example website'In [6]: response.xpath('//title/text()').extract()Out[6]: ['Example website']

同樣的我們也可以通過css選擇器獲取,例子如下:

In [7]: response.css('title::text')Out[7]: [<Selector xpath='descendant-or-self::title/text()' data='Example website'>]In [8]: response.css('title::text').extract_first()Out[8]: 'Example website'

查找圖片信息

這里通過xpath和css結(jié)合使用獲取圖片的src地址:

In [13]: response.xpath('//div[@id="images"]').css('img')Out[13]: [<Selector xpath='descendant-or-self::img' data='<img src="image1_thumb.jpg">'>, <Selector xpath='descendant-or-self::img' data='<img src="image2_thumb.jpg">'>, <Selector xpath='descendant-or-self::img' data='<img src="image3_thumb.jpg">'>, <Selector xpath='descendant-or-self::img' data='<img src="image4_thumb.jpg">'>, <Selector xpath='descendant-or-self::img' data='<img src="image5_thumb.jpg">'>]In [14]: response.xpath('//div[@id="images"]').css('img::attr(src)').extract()Out[14]: ['image1_thumb.jpg', 'image2_thumb.jpg', 'image3_thumb.jpg', 'image4_thumb.jpg', 'image5_thumb.jpg']

查找a標(biāo)簽信息

這里分別通過xapth和css選擇器獲取a標(biāo)簽的href內(nèi)容,以及文本信息,css獲取屬性信息是通過attr,xpath是通過@屬性名

In [15]: response.xpath('//a/@href')Out[15]: [<Selector xpath='//a/@href' data='image1.html'>, <Selector xpath='//a/@href' data='image2.html'>, <Selector xpath='//a/@href' data='image3.html'>, <Selector xpath='//a/@href' data='image4.html'>, <Selector xpath='//a/@href' data='image5.html'>]In [16]: response.xpath('//a/@href').extract()Out[16]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']In [17]: response.css('a::attr(href)')Out[17]: [<Selector xpath='descendant-or-self::a/@href' data='image1.html'>, <Selector xpath='descendant-or-self::a/@href' data='image2.html'>, <Selector xpath='descendant-or-self::a/@href' data='image3.html'>, <Selector xpath='descendant-or-self::a/@href' data='image4.html'>, <Selector xpath='descendant-or-self::a/@href' data='image5.html'>]In [18]: response.css('a::attr(href)').extract()Out[18]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']In [27]: response.css('a::text').extract()Out[27]: ['Name: My image 1 ', 'Name: My image 2 ', 'Name: My image 3 ', 'Name: My image 4 ', 'Name: My image 5 ']In [28]: response.xpath('//a/text()').extract()Out[28]: ['Name: My image 1 ', 'Name: My image 2 ', 'Name: My image 3 ', 'Name: My image 4 ', 'Name: My image 5 ']In [29]:

高級(jí)用法

查找屬性名稱包含img的所有的超鏈接,通過contains實(shí)現(xiàn)

In [36]: response.xpath('//a[contains(@href,"image")]/@href').extract()Out[36]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']In [37]: response.css('a[href*=image]::attr(href)').extract()Out[37]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']In [38]:

查找img的src屬性

In [41]: response.xpath('//a[contains(@href,"image")]/img/@src').extract()Out[41]: ['image1_thumb.jpg', 'image2_thumb.jpg', 'image3_thumb.jpg', 'image4_thumb.jpg', 'image5_thumb.jpg']In [42]: response.css('a[href*=image] img::attr(src)').extract()Out[42]: ['image1_thumb.jpg', 'image2_thumb.jpg', 'image3_thumb.jpg', 'image4_thumb.jpg', 'image5_thumb.jpg']In [43]:

提取a標(biāo)簽的文本中name后面的內(nèi)容,這里提供了正則的方法re和re_first

In [43]: response.css('a::text').re('Name:(.*)')Out[43]: [' My image 1 ', ' My image 2 ', ' My image 3 ', ' My image 4 ', ' My image 5 ']In [44]: response.css('a::text').re_first('Name:(.*)')Out[44]: ' My image 1 '


以上是全部?jī)?nèi)容,只是善于分享,不足之處請(qǐng)包涵!爬蟲基本的原理就是,獲取源碼,進(jìn)而獲取網(wǎng)頁內(nèi)容。一般來說,只要你給一個(gè)入口,通過分析,可以找到無限個(gè)其他相關(guān)的你需要的資源,進(jìn)而進(jìn)行爬取。


我也寫了很多其他的非常簡(jiǎn)單的入門級(jí)的爬蟲詳細(xì)教程,關(guān)注后,點(diǎn)擊我的頭像,就可以查看到。


歡迎大家一起留言討論和交流,謝謝!


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
小白學(xué) Python 爬蟲(35):爬蟲框架 Scrapy 入門基礎(chǔ)(三) Selector 選擇器
爬蟲:Scrapy5
Scrapy中選擇器的用法
爬蟲抓圖全網(wǎng)最新方法,這一次終于是4k高清美圖,只因?yàn)槲也幌螺dJPG圖片!
Python大佬批量爬取中國(guó)院士信息,告訴你哪個(gè)地方人杰地靈
網(wǎng)絡(luò)爬蟲之Scrapy實(shí)戰(zhàn)二:爬取多個(gè)網(wǎng)頁
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服