自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一名合格的數(shù)據(jù)分析師分享Python網(wǎng)絡(luò)爬蟲二三事(Scrapy自動爬蟲)

開發(fā) 開發(fā)工具
作為一名合格的數(shù)據(jù)分析師,其完整的技術(shù)知識體系必須貫穿數(shù)據(jù)獲取、數(shù)據(jù)存儲、數(shù)據(jù)提取、數(shù)據(jù)分析、數(shù)據(jù)挖掘、數(shù)據(jù)可視化等各大部分。

接上篇《一名合格的數(shù)據(jù)分析師分享Python網(wǎng)絡(luò)爬蟲二三事(綜合實戰(zhàn)案例)》

[[184080]]

五、綜合實戰(zhàn)案例

3. 利用Scrapy框架爬取

(1)了解Scrapy

 

Scrapy使用了Twisted異步網(wǎng)絡(luò)庫來處理網(wǎng)絡(luò)通訊。整體架構(gòu)大致如下(注:圖片來自互聯(lián)網(wǎng)):

Scrapy整體架構(gòu)

關(guān)于Scrapy的使用方法請參考其官方文檔

(2)Scrapy自動爬蟲

 

前面的實戰(zhàn)中我們都是通過循環(huán)構(gòu)建URL進行數(shù)據(jù)爬取,其實還有另外一種實現(xiàn)方式,首先設(shè)定初始URL,獲取當(dāng)前URL中的新鏈接,基于這些鏈接繼續(xù)爬取,直到所爬取的頁面不存在新的鏈接為止。

(a)需求

采用自動爬蟲的方式爬取糗事百科文章鏈接與內(nèi)容,并將文章頭部內(nèi)容與鏈接存儲到MySQL數(shù)據(jù)庫中。

(b)分析

A. 怎么提取首頁文章鏈接?

打開首頁后查看源碼,搜索首頁任一篇文章內(nèi)容,可以看到"/article/118123230"鏈接,點擊進去后發(fā)現(xiàn)這就是我們所要的文章內(nèi)容,所以我們在自動爬蟲中需設(shè)置鏈接包含"article"

提取首頁文章鏈接

B. 怎么提取詳情頁文章內(nèi)容與鏈接

  • 內(nèi)容

打開詳情頁后,查看文章內(nèi)容如下:

分析可知利用包含屬性class且其值為content的div標(biāo)簽可***確定文章內(nèi)容,表達式如下:

  1. "//div[@class='content']/text()" 
  • 鏈接

打開任一詳情頁,復(fù)制詳情頁鏈接,查看詳情頁源碼,搜索鏈接如下:

采用以下XPath表達式可提取文章鏈接。

  1. ["//link[@rel='canonical']/@href"]  

(3)項目源碼

A. 創(chuàng)建爬蟲項目

打開CMD,切換到存儲爬蟲項目的目錄下,輸入:

  1. scrapy startproject qsbkauto 

B. 項目結(jié)構(gòu)說明

  • spiders.qsbkspd.py:爬蟲文件
  • items.py:項目實體,要提取的內(nèi)容的容器,如當(dāng)當(dāng)網(wǎng)商品的標(biāo)題、評論數(shù)等
  • pipelines.py:項目管道,主要用于數(shù)據(jù)的后續(xù)處理,如將數(shù)據(jù)寫入Excel和db等
  • settings.py:項目設(shè)置,如默認(rèn)是不開啟pipeline、遵守robots協(xié)議等
  • scrapy.cfg:項目配置

C. 創(chuàng)建爬蟲

進入創(chuàng)建的爬蟲項目,輸入:

  1. scrapy genspider -t crawl qsbkspd qiushibaie=ke.com(域名) 

D. 定義items

  1. import scrapyclass QsbkautoItem(scrapy.Item): 
  2.     # define the fields for your item here like: 
  3.     # name = scrapy.Field() 
  4.     Link = scrapy.Field()     #文章鏈接 
  5.     Connent = scrapy.Field()  #文章內(nèi)容 
  6.     pass 

E. 編寫爬蟲

  • qsbkauto.py
    1. # -*- coding: utf-8 -*-import scrapyfrom scrapy.linkextractors import LinkExtractorfrom scrapy.spiders import CrawlSpider, Rulefrom qsbkauto.items import QsbkautoItemfrom scrapy.http import Requestclass QsbkspdSpider(CrawlSpider): 
    2.   name = 'qsbkspd' 
    3.   allowed_domains = ['qiushibaike.com'] 
    4.   #start_urls = ['http://qiushibaike.com/'] 
    5.   def start_requests(self): 
    6.       i_headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0"} 
    7.       yield Request('http://www.qiushibaike.com/',headers=i_headers
    8.   rules = ( 
    9.       Rule(LinkExtractor(allow=r'article/'), callback='parse_item'follow=True), 
    10.   ) 
    11.   def parse_item(self, response): 
    12.       #i = {} 
    13.       #i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract() 
    14.       #i['name'] = response.xpath('//div[@id="name"]').extract() 
    15.       #i['description'] = response.xpath('//div[@id="description"]').extract() 
    16.       i = QsbkautoItem() 
    17.       i["content"]=response.xpath("//div[@class='content']/text()").extract() 
    18.       i["link"]=response.xpath("//link[@rel='canonical']/@href").extract() 
    19.       return i 

pipelines.py

  1. import MySQLdbimport timeclass QsbkautoPipeline(object): 
  2.   def exeSQL(self,sql): 
  3.       ''' 
  4.       功能:連接MySQL數(shù)據(jù)庫并執(zhí)行sql語句 
  5.       @sql:定義SQL語句 
  6.       ''' 
  7.       con = MySQLdb.connect( 
  8.           host='localhost',  # port 
  9.           user='root',       # usr_name 
  10.           passwd='xxxx',     # passname 
  11.           db='spdRet',       # db_name 
  12.           charset='utf8'
  13.           local_infile = 1 
  14.           ) 
  15.       con.query(sql) 
  16.       con.commit() 
  17.       con.close() 
  18.   def process_item(self, item, spider): 
  19.       link_url = item['link'][0] 
  20.       content_header = item['content'][0][0:10] 
  21.       curr_date = time.strftime('%Y-%m-%d',time.localtime(time.time())) 
  22.       content_header = curr_date+'__'+content_header 
  23.       if (len(link_url) and len(content_header)):#判斷是否為空值 
  24.           try: 
  25.               sql="insert into qiushi(content,link) values('"+content_header+"','"+link_url+"')" 
  26.               self.exeSQL(sql) 
  27.           except Exception as er: 
  28.               print("插入錯誤,錯誤如下:") 
  29.               print(er) 
  30.       else: 
  31.           pass 
  32.       return item 
  • setting.py
  1. 關(guān)閉ROBOTSTXT_OBEY
  2. 設(shè)置USER_AGENT
  3. 開啟ITEM_PIPELINES

F. 執(zhí)行爬蟲

  1. scrapy crawl qsbkauto --nolog 

G. 結(jié)果

【本文是51CTO專欄機構(gòu)“豈安科技”的原創(chuàng)文章,轉(zhuǎn)載請通過微信公眾號(bigsec)聯(lián)系原作者】

戳這里,看該作者更多好文

責(zé)任編輯:趙寧寧 來源: 51CTO專欄
相關(guān)推薦

2017-02-23 17:46:11

數(shù)據(jù)分析師Python網(wǎng)絡(luò)爬蟲

2017-02-23 18:24:14

數(shù)據(jù)分析師Python網(wǎng)絡(luò)爬蟲

2015-08-04 13:25:46

數(shù)據(jù)分析

2014-06-19 14:00:46

數(shù)據(jù)分析師

2017-12-11 15:56:44

數(shù)據(jù)分析師數(shù)據(jù)倉庫數(shù)據(jù)源

2017-09-19 13:35:47

數(shù)據(jù)科學(xué)數(shù)據(jù)分析python

2016-11-11 20:38:39

數(shù)據(jù)分析師大數(shù)據(jù)

2019-05-15 15:57:15

Python數(shù)據(jù)分析爬蟲

2012-06-08 10:12:08

架構(gòu)師

2020-04-27 09:25:16

Python爬蟲庫數(shù)據(jù)科學(xué)

2021-09-26 05:01:55

Scrapy項目爬蟲

2018-08-08 11:40:24

ScrapyRequest網(wǎng)絡(luò)爬蟲

2013-04-11 10:03:55

2017-11-29 15:21:53

PythonScrapy爬蟲

2012-08-08 09:00:29

數(shù)據(jù)分析師

2015-08-18 13:26:05

數(shù)據(jù)分析

2021-01-08 09:07:19

Scrapy框架爬蟲

2017-05-15 21:00:15

大數(shù)據(jù)Scrapy爬蟲框架

2018-12-05 13:59:45

電影分析爬蟲

2015-09-30 09:36:58

數(shù)據(jù)分析師面試offer
點贊
收藏

51CTO技術(shù)棧公眾號