欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報邀請碼778899分享:爬蟲框架scrapy基本原理

柚子快報邀請碼778899分享:爬蟲框架scrapy基本原理

http://yzkb.51969.com/

一、scrapy介紹和快速使用

scrapy是python的爬蟲框架,類似于django(python的web框架)。

安裝:

Mac、Linux 執(zhí)行pip3 install scrapy,不存在任何問題

Windows 執(zhí)行pip3 install scrapy,如果安裝失敗,執(zhí)行下面步驟: (1)安裝wheel(為支持通過文件安裝軟件):pip3 install wheel(wheel官網(wǎng)) (2)安裝lxml:pip3 install lxml (3)安裝pyopenssl:pip3 install pyopenssl (4)下載并安裝pywin32(pywin32官網(wǎng) 或 github地址) (5)下載twisted的wheel文件(twisted官網(wǎng)),執(zhí)行pip3 install 下載目錄\Twisted-17.9.0-cp36-cp36m-win_amd64.whl (6)最后執(zhí)行pip3 install scrapy

注:twisted是一個流行的事件驅(qū)動的python網(wǎng)絡框架,性能很高的網(wǎng)絡框架。

使用: 1 使用命令行創(chuàng)建scrapy項目

scrapy startproject 項目名

2 創(chuàng)建爬蟲(相當于django創(chuàng)建app)

scrapy genspider 爬蟲名 爬蟲地址

3 運行爬蟲

scrapy crawl 爬蟲名

4 使用IDE打開爬蟲項目(項目根目錄)

5 目錄結(jié)構(gòu)

myfirst_crawl # 項目名

myfirst_crawl # 文件夾

-spiders # 文件夾(所有的爬蟲都放在下面,一個py文件就是一個爬蟲)

-cnblogs.py # 爬蟲

middlewares.py # 以后中間件寫在這里

pipelines.py # 以后存數(shù)據(jù)

settings.py # 配置文件

scrapy.cfg # 項目部署相關

items.py # 相當于models.py

二、scrapy框架原理

五大組件 spider:爬蟲,我們寫代碼的地方,爬取網(wǎng)址和解析數(shù)據(jù) engine:引擎,大總管,掌管數(shù)據(jù)的流向(我們不管) scheduler:調(diào)度器,負責調(diào)度哪個地址先爬取,哪個后爬取(深度優(yōu)先,廣度優(yōu)先) downloader:下載器,負責真正的下載(twisted,異步) pipeline:管道,存儲數(shù)據(jù)的地方(文件,mysql、redis…)

兩大中間件 爬蟲中間件:爬蟲和引擎之間 下載中間件:引擎和下載器之間(用的多)

三、scrapy解析數(shù)據(jù)

1.執(zhí)行命令 直接敲這個名令:scrapy gensipder 名字 地址,等同于新建一個py文件 執(zhí)行爬蟲,不打印日志:scrapy crawl cnblogs --nolog

使用腳本運行爬蟲: 項目根目錄下創(chuàng)建main.py

# 腳本執(zhí)行爬蟲,不用使用命令了

from scrapy.cmdline import execute

execute(['scrapy','crawl','cnblogs','--nolog'])

2.response對象的css方法和xpath方法 css中寫css選擇器,xpath中寫xpath選擇器

語法: -xpath取文本內(nèi)容

'.//a[contains(@class,"link-title")]/text()'

-xpath取屬性

'.//a[contains(@class,"link-title")]/@href'

-css取文本

'a.link-title::text'

-css取屬性

'img.image-scale::attr(src)'

兩個方法:

.extract_first() # 取一個

.extract() # 取所有

spiders目錄下爬蟲文件參數(shù):

import scrapy

class CnblogsSpider(scrapy.Spider):

name = 'cnblogs' # 爬蟲名

allowed_domains = ['www.cnblogs.com'] # 允許爬取的域

start_urls = ['http://www.cnblogs.com/'] # 爬取的起始地址

def parse(self, response): # 響應對象response,從中解析出想要的數(shù)據(jù)

print('--------------',response)

print(response.text) # bs4解析

示例: spiders目錄下的cnblogs.py文件

import scrapy

class CnblogsSpider(scrapy.Spider):

name = 'cnblogs' # 爬蟲名

allowed_domains = ['www.cnblogs.com'] # 允許爬取的域

start_urls = ['http://www.cnblogs.com/'] # 爬取的起始地址

def parse(self, response): # 響應對象response,從中解析出想要的數(shù)據(jù)

# print('--------------',response)

# print(response.text) # bs4解析

############ css ###########

# article_list=response.css('.post-item') # 查出所有類名為post-item的標簽,取多條

# # print(len(article_list))

# for article in article_list:

# title=article.css('div.post-item-text>a::text').extract_first() # 取一條

# # article.css('section>div>a::text')

# href=article.css('div.post-item-text>a::attr(href)').extract_first()

# author=article.css('a.post-item-author>span::text').extract_first()

# desc=article.css('p.post-item-summary::text').extract_first()

#

# # 取不到就是None

# photo=article.css('p.post-item-summary>a>img::attr(src)').extract_first()

#

#

# print(title)

# print(href)

# print(author)

# print(desc)

# print(photo)

# print('----------')

########### xpath ###########

article_list = response.xpath('//*[@class="post-item"]') # 查出所有類名為post-item的標簽,取多條

# print(len(article_list))

for article in article_list:

# 注意:使用 . 從當前標簽下找

title = article

柚子快報邀請碼778899分享:爬蟲框架scrapy基本原理

http://yzkb.51969.com/

推薦文章

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19275159.html

發(fā)布評論

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄