摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料標(biāo)簽選擇器對象創(chuàng)建標(biāo)簽選擇器對象,參數(shù)接收回調(diào)的對象需要導(dǎo)入模塊標(biāo)簽選擇器方法,是里的一個方法,參數(shù)接收選擇器規(guī)則,返回列表元素是一個標(biāo)簽對象獲取到選擇器過濾后的內(nèi)容,返回列表元素是內(nèi)容選擇器規(guī)則表示
標(biāo)簽選擇器對象
HtmlXPathSelector()創(chuàng)建標(biāo)簽選擇器對象,參數(shù)接收response回調(diào)的html對象
需要導(dǎo)入模塊:from scrapy.selector import HtmlXPathSelector
select()標(biāo)簽選擇器方法,是HtmlXPathSelector里的一個方法,參數(shù)接收選擇器規(guī)則,返回列表元素是一個標(biāo)簽對象
extract()獲取到選擇器過濾后的內(nèi)容,返回列表元素是內(nèi)容
選擇器規(guī)則
//x?表示向下查找n層指定標(biāo)簽,如://div 表示查找所有div標(biāo)簽
/x?表示向下查找一層指定的標(biāo)簽
/@x?表示查找指定屬性,可以連綴如:@id @src
[@class="class名稱"]?表示查找指定屬性等于指定值的標(biāo)簽,可以連綴 ,查找class名稱等于指定名稱的標(biāo)簽
/text()?獲取標(biāo)簽文本類容
[x]?通過索引獲取集合里的指定一個元素
獲取指定的標(biāo)簽對象
#?-*-?coding:?utf-8?-*- import?scrapy???????#導(dǎo)入爬蟲模塊 from?scrapy.selector?import?HtmlXPathSelector??#導(dǎo)入HtmlXPathSelector模塊 from?urllib?import?request?????????????????????#導(dǎo)入request模塊 import?os class?AdcSpider(scrapy.Spider): ????name?=?"adc"????????????????????????????????????????#設(shè)置爬蟲名稱 ????allowed_domains?=?["www.shaimn.com"] ????start_urls?=?["http://www.shaimn.com/xinggan/"] ????def?parse(self,?response): ????????hxs?=?HtmlXPathSelector(response)???????????????#創(chuàng)建HtmlXPathSelector對象,將頁面返回對象傳進(jìn)去 ????????items?=?hxs.select("http://div[@class="showlist"]/li")??#標(biāo)簽選擇器,表示獲取所有class等于showlist的div,下面的li標(biāo)簽 ????????print(items)???????????????????????????????????????#返回標(biāo)簽對象
循環(huán)獲取到每個li標(biāo)簽里的子標(biāo)簽,以及各種屬性或者文本
#?-*-?coding:?utf-8?-*- import?scrapy???????#導(dǎo)入爬蟲模塊 from?scrapy.selector?import?HtmlXPathSelector??#導(dǎo)入HtmlXPathSelector模塊 from?urllib?import?request?????????????????????#導(dǎo)入request模塊 import?os class?AdcSpider(scrapy.Spider): ????name?=?"adc"????????????????????????????????????????#設(shè)置爬蟲名稱 ????allowed_domains?=?["www.shaimn.com"] ????start_urls?=?["http://www.shaimn.com/xinggan/"] ????def?parse(self,?response): ????????hxs?=?HtmlXPathSelector(response)???????????????#創(chuàng)建HtmlXPathSelector對象,將頁面返回對象傳進(jìn)去 ????????items?=?hxs.select("http://div[@class="showlist"]/li")??#標(biāo)簽選擇器,表示獲取所有class等于showlist的div,下面的li標(biāo)簽 ????????#?print(items)?????????????????????????????????????#返回標(biāo)簽對象 ????????for?i?in?range(len(items)):????????????????????????#根據(jù)li標(biāo)簽的長度循環(huán)次數(shù) ????????????title?=?hxs.select("http://div[@class="showlist"]/li[%d]//img/@alt"?%?i).extract()???#根據(jù)循環(huán)的次數(shù)作為下標(biāo)獲取到當(dāng)前l(fā)i標(biāo)簽,下的img標(biāo)簽的alt屬性內(nèi)容 ????????????src?=?hxs.select("http://div[@class="showlist"]/li[%d]//img/@src"?%?i).extract()?????#根據(jù)循環(huán)的次數(shù)作為下標(biāo)獲取到當(dāng)前l(fā)i標(biāo)簽,下的img標(biāo)簽的src屬性內(nèi)容 ????????????if?title?and?src: ????????????????print(title,src)??#返回類容列表
將獲取到的圖片下載到本地
urlretrieve()將文件保存到本地,參數(shù)1要保存文件的src,參數(shù)2保存路徑
urlretrieve是urllib下request模塊的一個方法,需要導(dǎo)入from urllib import request
#?-*-?coding:?utf-8?-*- import?scrapy???????#導(dǎo)入爬蟲模塊 from?scrapy.selector?import?HtmlXPathSelector??#導(dǎo)入HtmlXPathSelector模塊 from?urllib?import?request?????????????????????#導(dǎo)入request模塊 import?os class?AdcSpider(scrapy.Spider): ????name?=?"adc"????????????????????????????????????????#設(shè)置爬蟲名稱 ????allowed_domains?=?["www.shaimn.com"] ????start_urls?=?["http://www.shaimn.com/xinggan/"] ????def?parse(self,?response): ????????hxs?=?HtmlXPathSelector(response)???????????????#創(chuàng)建HtmlXPathSelector對象,將頁面返回對象傳進(jìn)去 ????????items?=?hxs.select("http://div[@class="showlist"]/li")??#標(biāo)簽選擇器,表示獲取所有class等于showlist的div,下面的li標(biāo)簽 ????????#?print(items)?????????????????????????????????????#返回標(biāo)簽對象 ????????for?i?in?range(len(items)):????????????????????????#根據(jù)li標(biāo)簽的長度循環(huán)次數(shù) ????????????title?=?hxs.select("http://div[@class="showlist"]/li[%d]//img/@alt"?%?i).extract()???#根據(jù)循環(huán)的次數(shù)作為下標(biāo)獲取到當(dāng)前l(fā)i標(biāo)簽,下的img標(biāo)簽的alt屬性內(nèi)容 ????????????src?=?hxs.select("http://div[@class="showlist"]/li[%d]//img/@src"?%?i).extract()?????#根據(jù)循環(huán)的次數(shù)作為下標(biāo)獲取到當(dāng)前l(fā)i標(biāo)簽,下的img標(biāo)簽的src屬性內(nèi)容 ????????????if?title?and?src: ????????????????#?print(title[0],src[0])????????????????????????????????????????????????????#通過下標(biāo)獲取到字符串內(nèi)容 ????????????????file_path?=?os.path.join(os.getcwd()?+?"/img/",?title[0]?+?".jpg")??????????#拼接圖片保存路徑 ????????????????request.urlretrieve(src[0],?file_path)??????????????????????????#將圖片保存到本地,參數(shù)1獲取到的src,參數(shù)2保存路徑
xpath()標(biāo)簽選擇器,是Selector類里的一個方法,參數(shù)是選擇規(guī)則【推薦】
選擇器規(guī)則同上
selector()創(chuàng)建選擇器類,需要接受html對象
需要導(dǎo)入:from scrapy.selector import Selector
#?-*-?coding:?utf-8?-*- import?scrapy???????#導(dǎo)入爬蟲模塊 from?scrapy.selector?import?HtmlXPathSelector??#導(dǎo)入HtmlXPathSelector模塊 from?scrapy.selector?import?Selector class?AdcSpider(scrapy.Spider): ????name?=?"adc"????????????????????????????????????????#設(shè)置爬蟲名稱 ????allowed_domains?=?["www.shaimn.com"] ????start_urls?=?["http://www.shaimn.com/xinggan/"] ????def?parse(self,?response): ????????items?=?Selector(response=response).xpath("http://div[@class="showlist"]/li").extract() ????????#?print(items)?????????????????????????????????????#返回標(biāo)簽對象 ????????for?i?in?range(len(items)): ????????????title?=?Selector(response=response).xpath("http://div[@class="showlist"]/li[%d]//img/@alt"?%?i).extract() ????????????src?=?Selector(response=response).xpath("http://div[@class="showlist"]/li[%d]//img/@src"?%?i).extract() ????????????print(title,src)
正則表達(dá)式的應(yīng)用
正則表達(dá)式是彌補(bǔ),選擇器規(guī)則無法滿足過濾情況時使用的,
分為兩種正則使用方式
1、將選擇器規(guī)則過濾出來的結(jié)果進(jìn)行正則匹配
2、在選擇器規(guī)則里應(yīng)用正則進(jìn)行過濾
1、將選擇器規(guī)則過濾出來的結(jié)果進(jìn)行正則匹配,用正則取最終內(nèi)容
最后.re("正則")
#?-*-?coding:?utf-8?-*- import?scrapy???????#導(dǎo)入爬蟲模塊 from?scrapy.selector?import?HtmlXPathSelector??#導(dǎo)入HtmlXPathSelector模塊 from?scrapy.selector?import?Selector class?AdcSpider(scrapy.Spider): ????name?=?"adc"????????????????????????????????????????#設(shè)置爬蟲名稱 ????allowed_domains?=?["www.shaimn.com"] ????start_urls?=?["http://www.shaimn.com/xinggan/"] ????def?parse(self,?response): ????????items?=?Selector(response=response).xpath("http://div[@class="showlist"]/li//img")[0].extract() ????????print(items)?????????????????????????????????????#返回標(biāo)簽對象 ????????items2?=?Selector(response=response).xpath("http://div[@class="showlist"]/li//img")[0].re("alt="(w+)") ????????print(items2) #? #?["人體藝術(shù)mmSunny前凸后翹性感誘惑寫真"]
2、在選擇器規(guī)則里應(yīng)用正則進(jìn)行過濾
[re:正則規(guī)則]
#?-*-?coding:?utf-8?-*- import?scrapy???????#導(dǎo)入爬蟲模塊 from?scrapy.selector?import?HtmlXPathSelector??#導(dǎo)入HtmlXPathSelector模塊 from?scrapy.selector?import?Selector class?AdcSpider(scrapy.Spider): ????name?=?"adc"????????????????????????????????????????#設(shè)置爬蟲名稱 ????allowed_domains?=?["www.shaimn.com"] ????start_urls?=?["http://www.shaimn.com/xinggan/"] ????def?parse(self,?response): ????????items?=?Selector(response=response).xpath("http://div").extract() ????????#?print(items)?????????????????????????????????????#返回標(biāo)簽對象 ????????items2?=?Selector(response=response).xpath("http://div[re:test(@class,?"showlist")]").extract()??#正則找到div的class等于showlist的元素 ????????print(items2)
【轉(zhuǎn)載自:http://www.leiqiankun.com/?id=47】
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/44026.html
摘要:什么是爬蟲網(wǎng)絡(luò)爬蟲也叫網(wǎng)絡(luò)蜘蛛,是一種自動化瀏覽網(wǎng)絡(luò)的程序,或者說是一種網(wǎng)絡(luò)機(jī)器人。 什么是爬蟲 網(wǎng)絡(luò)爬蟲也叫網(wǎng)絡(luò)蜘蛛,是一種自動化瀏覽網(wǎng)絡(luò)的程序,或者說是一種網(wǎng)絡(luò)機(jī)器人。它們被廣泛用于互聯(lián)網(wǎng)搜索引擎或其他類似網(wǎng)站,以獲取或更新這些網(wǎng)站的內(nèi)容和檢索方式。它們可以自動采集所有其能夠訪問到的頁面內(nèi)容,以供搜索引擎做進(jìn)一步處理(分檢整理下載的頁面),而使得用戶能更快的檢索到他們需要的信息。簡...
摘要:百度云搜索,搜各種資料搜網(wǎng)盤,搜各種資料表達(dá)式表示向下查找層指定標(biāo)簽,如表示查找所有標(biāo)簽表示向下查找一層指定的標(biāo)簽表示查找指定屬性的值可以連綴如屬性名稱屬性值表示查找指定屬性等于指定值的標(biāo)簽可以連綴,如查找名稱等于指定名稱的標(biāo)簽獲取標(biāo)簽文本 【百度云搜索,搜各種資料:http://www.lqkweb.com】 【搜網(wǎng)盤,搜各種資料:http://www.swpan.cn】 xpath...
摘要:是最有名的爬蟲框架之一,可以很方便的進(jìn)行抓取,并且提供了很強(qiáng)的定制型,這里記錄簡單學(xué)習(xí)的過程和在實際應(yīng)用中會遇到的一些常見問題一安裝在安裝之前有一些依賴需要安裝,否則可能會安裝失敗,的選擇器依賴于,還有網(wǎng)絡(luò)引擎,下面是下安裝的過程下安裝安裝 scrapy是python最有名的爬蟲框架之一,可以很方便的進(jìn)行web抓取,并且提供了很強(qiáng)的定制型,這里記錄簡單學(xué)習(xí)的過程和在實際應(yīng)用中會遇到的一...
摘要:通用網(wǎng)絡(luò)爬蟲通用網(wǎng)絡(luò)爬蟲又稱全網(wǎng)爬蟲,爬取對象從一些種子擴(kuò)充到整個。為提高工作效率,通用網(wǎng)絡(luò)爬蟲會采取一定的爬取策略。介紹是一個國人編寫的強(qiáng)大的網(wǎng)絡(luò)爬蟲系統(tǒng)并帶有強(qiáng)大的。 爬蟲 簡單的說網(wǎng)絡(luò)爬蟲(Web crawler)也叫做網(wǎng)絡(luò)鏟(Web scraper)、網(wǎng)絡(luò)蜘蛛(Web spider),其行為一般是先爬到對應(yīng)的網(wǎng)頁上,再把需要的信息鏟下來。 分類 網(wǎng)絡(luò)爬蟲按照系統(tǒng)結(jié)構(gòu)和實現(xiàn)技術(shù),...
摘要:通用網(wǎng)絡(luò)爬蟲通用網(wǎng)絡(luò)爬蟲又稱全網(wǎng)爬蟲,爬取對象從一些種子擴(kuò)充到整個。為提高工作效率,通用網(wǎng)絡(luò)爬蟲會采取一定的爬取策略。介紹是一個國人編寫的強(qiáng)大的網(wǎng)絡(luò)爬蟲系統(tǒng)并帶有強(qiáng)大的。 爬蟲 簡單的說網(wǎng)絡(luò)爬蟲(Web crawler)也叫做網(wǎng)絡(luò)鏟(Web scraper)、網(wǎng)絡(luò)蜘蛛(Web spider),其行為一般是先爬到對應(yīng)的網(wǎng)頁上,再把需要的信息鏟下來。 分類 網(wǎng)絡(luò)爬蟲按照系統(tǒng)結(jié)構(gòu)和實現(xiàn)技術(shù),...
閱讀 555·2021-08-31 09:45
閱讀 1666·2021-08-11 11:19
閱讀 898·2019-08-30 15:55
閱讀 836·2019-08-30 10:52
閱讀 2872·2019-08-29 13:11
閱讀 2940·2019-08-23 17:08
閱讀 2851·2019-08-23 15:11
閱讀 3080·2019-08-23 14:33