天天看點

Scrapy架構爬取詳細步驟

Scrapy架構

(本文隻做學習使用,請勿他用)

1.需求工具 pycharm 小說網的域名 (www.qisuu.com)

第一步—–建立檔案

Scrapy架構爬取詳細步驟

建立成功後顯示如圖:

Scrapy架構爬取詳細步驟

第二步——将建立在桌面上的scrapy檔案用pycharm打開:

這是建立成功後在pycharm中的顯示

Scrapy架構爬取詳細步驟

pycharm左下角打開 Terminal

Scrapy架構爬取詳細步驟

打開後如圖 我第一次鍵入了一條指令 提示爬蟲名字不能和項目名稱一樣,更改後再運作..成功建立爬蟲檔案 booksspider

建立爬蟲檔案指令: scrapy+ genspider+ 蜘蛛名稱 +網站域名

Scrapy架構爬取詳細步驟

建立成功後,出現爬蟲檔案:

Scrapy架構爬取詳細步驟

接下來,就可以在爬蟲檔案中寫爬蟲代碼了

第三步——–編寫爬蟲代碼

1.紅框框起來的頭部 有一個是自帶的,我提前 引用了幾個 接下來我需要用到的 功能子產品在這裡就不再詳細解釋子產品功能,下文用到後再解釋.

2.橢圓裡面的内容 填寫你爬取開始的頁面URL,這裡是自動生成的,一般是不正确的,需要自己打開要爬取的初始頁,将URL複制到這裡.

Scrapy架構爬取詳細步驟

3.代碼思路

1)請求導覽列上的每個按鈕對應的頁面

2)分别解析每個頁面的電子書清單(主要獲得電子書的詳情url)

3)請求詳情url,解析電子書的詳細資訊(書名,封面,評分,大小…下載下傳位址)

4)根據下載下傳位址下載下傳電子書到本地

擷取導航欄文字及連結

def parse(self, response):
        a_list = response.xpath("//div[@class='nav']/a[@target='_blank']")
        for a in a_list:
            # 分類名稱
            category_name = a.xpath("text()").extract_first("")
            # 拼接完整的分類url
            category_url = urlparse.urljoin(response.url, a.xpath("@href").extract_first(""))
            # 将分類位址轉發給downloader下載下傳并将結果傳給parse_books_list
            # meta:專門用來傳遞參數,類型是字典
            yield scrapy.Request(
                url=category_url,
                callback=self.parse_books_list,
                meta={"category_name": category_name, }
            )
           

擷取每本書連結

def parse_books_list(self, response):
        href_list = response.xpath("//div[@class='listBox']/ul/li/a/@href").extract()
        for href in href_list:
            list_href = urlparse.urljoin(response.url, href)
            yield scrapy.Request(
                url=list_href,
                callback=self.parse_books_detail,
                meta=response.meta,
                # meta={"category_name": response.meta['category_name'],}
            )
        all_pages = response.xpath("//select[@name='select']/option/@value").extract()
        for page in all_pages:
            detail_url = urlparse.urljoin(response.url, page)
            yield scrapy.Request(
                url=detail_url,
                callback=self.parse_books_list,
                meta=response.meta
            )
           

進入書本詳細頁 擷取書本詳細資訊及 下載下傳連結 封面連結

def parse_books_detail(self, response):
        info_div = response.xpath("//div[@class='detail_right']")
        title = info_div.xpath("h1/text()").extract_first("")
        li_list = info_div.xpath("ul/li")
        size = li_list[].xpath("text()").extract_first("")
        size = size.replace(u"檔案大小:", "").strip()
        date_time = li_list[].xpath("text()").extract_first("")
        date_time = date_time.replace(u"釋出日期:", "").strip()
        user = li_list[].xpath("a/text()").extract_first("")
        download_times = li_list[].xpath("text()").extract_first("")
        download_times = download_times.replace(u"下載下傳次數:", "").strip()
        book_degree = li_list[].xpath("em/@class").extract_first("")
        book_degree = book_degree.replace("lstar", "").strip()
        download_url = response.xpath("//a[@class='downButton']/@href")[].extract()
        img_url = response.xpath("//div[@class='detail_pic']/img/@src").extract_first("")
        img_url = urlparse.urljoin(response.url, img_url)
        category_name = response.meta['category_name']
        print title, user, date_time, category_name

        item = BooksItem()
        item['title'] = title
        item['size'] = size
        item['date_time'] = date_time
        item['user'] = user
        item['download_times'] = download_times
        item['book_degree'] = book_degree
        # 小說要以GBK格式進行存儲
        ########################
        item['download_url'] = [u"%s" % download_url]
        item['img_url'] = [img_url]
        ########################注意以清單方式存儲
        item['category_name'] = category_name
        yield item
           

第四步——設定item /與存儲有關

将需要存儲的資訊寫入,如圖所示:

Scrapy架構爬取詳細步驟

第五步——配置settings /與下載下傳有關

打開settings,找到紅方框中代碼,原本是被注釋掉的,将 ITEM_PIPELINES{} 解注釋.原本就有的内容注釋掉,另外添加兩條與下載下傳圖檔與文本的代碼. 最後,在ITEM_PIPELINES{}下面鍵入四行代碼,分别為圖檔和文本的下載下傳連結與存儲路徑

Scrapy架構爬取詳細步驟

第六步——在Terminal中輸入運作指令

scrapy+crawl+爬蟲名稱

Scrapy架構爬取詳細步驟

運作後就會出現這個啦,圖檔和文字全部存入這兩個檔案夾中

Scrapy架構爬取詳細步驟

以上内容為 比較粗糙,因為本人也不太熟,僅做參考.懶癌犯了~ 以後完善

繼續閱讀