Scrapy——6
- 怎樣進行APP抓包
- scrapy架構抓取APP豆果美食資料
- 怎樣用scrapy架構下載下傳圖檔
- 怎樣用scrapy架構去下載下傳鬥魚APP的圖檔?
- Scrapy建立下載下傳圖檔常見那些問題
怎樣進行APP抓包? 1.連接配接網絡
- 安裝fiddler,并且進行配置:
Tools >> options >> connections >> 勾選 allow remote computers to connect
-
檢視本機ip位址:
在cmd視窗中,輸入 ipconfig ,檢視 以太網 ,可以看到
IPv4 位址...............:192.168.0.104
這個192.168.*.***(192.168.0.104) 就是你的本機IP
- 確定手機連接配接了wifi,并且和電腦是在同一個區域網路,
- 設定代理,在WiFi中長按連接配接的wifi選擇設定代理

- 在手機中,打開浏覽器,通路
http://10.209.143:1234
IP:是第二步檢視到的ip位址,替換成你自己的IP
port:8888是你在fiddler中配置的
注意:有些浏覽器會顯示打不開,更換其他浏覽器就可以了
怎樣進行APP抓包? 2.通路網絡
打開後點選最後的連結(光标處),進行證書安裝就可以了
怎樣進行APP抓包? 3.安裝證書
- 安裝 證書
部分手機可以直接點選 安裝
部分手機需要 設定 >> wifi(或WLAN) >> 進階設定 >> 安裝證書 >>
選中剛剛下載下傳的 證書檔案 FiddlerRoot.cer >> 确定
設定(Settings) >> 更多設定 >> 系統安全 >> 從儲存設備安裝
為證書命名 , 輸入自己喜歡的名字,譬如 fiddler ,确定 , 顯示 證書安裝完成
安裝完成後,在 設定(Settings) >> 更多設定 >> 系統安全 >> 信任的憑證 >>
系統和使用者2個tab頁 >> 使用者 >> 可以檢視到 DO_NOT_RUST_FiddlerRoot
PS: 不安裝證書,抓取http的資料是沒問題的,但是抓取不了https的資料
怎樣進行APP抓包? 4.手機抓包
注意:
1、大部分app都可以直接抓包
2、少部分app沒辦法直接擷取,需要 wireshark、反編譯、脫殼 等方式去查找加密算法
3、app抓包一般都是抓取到伺服器傳回的json資料包
scrapy架構抓取APP豆果美食資料
手機打開豆果美食APP,同時打開fiddler,浏覽你需要爬取的資料頁面,然後就可以在fiddler中分析抓取的網絡請求
因為手機資料一般都是json格式的資料,是以多注意網絡請求的格式即可
很快就找到了我們需要的請求,接下來就用scrapy模拟請求解析資料
- 建立項目
- Local/Scrapy/douguo/douguo/items.py 設定需要儲存的資料(作者、菜名、用時、難度、前言、圖檔連結)
import scrapy class DouguoItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() auth = scrapy.Field() cook_name = scrapy.Field() cook_time = scrapy.Field() cook_difficulty = scrapy.Field() cook_story = scrapy.Field() img = scrapy.Field()
- Local/Scrapy/douguo/douguo/settings.py 設定爬蟲協定
Local/Scrapy/douguo/douguo/spiders/douguojiachang.py 編寫代碼# Obey robots.txt rules ROBOTSTXT_OBEY = False
- 根據網絡請求資料的方法,進行post請求,自行分析需要的請求頭
- scrapy擷取json資料,用的是response.body,再用json.dumps()轉換
# -*- coding: utf-8 -*- import scrapy import json from ..items import DouguoItem class DouguoJiachangSpider(scrapy.Spider): name = 'douguo_jiachang' # allowed_domains = ['baidu.com'] # start_urls = ['http://api.douguo.net/recipe/v2/search/0/20'] page = 0 def start_requests(self): base_url = 'http://api.douguo.net/recipe/v2/search/{}/20' url = base_url.format(self.page) data = { 'client': '4', '_session': '1542354711458863254010224946', 'keyword': '家常菜', 'order': '0', '_vs': '400' } self.page += 20 yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse) def parse(self, response): date = json.loads(response.body.decode()) # 将json格式資料轉換成字典 t = date.get('result').get('list') for i in t: douguo_item = DouguoItem() douguo_item['auth'] = i.get('r').get('an') douguo_item['cook_name'] = i.get('r').get('n') douguo_item['cook_time'] = i.get('r').get('cook_time') douguo_item['cook_difficulty'] = i.get('r').get('cook_difficulty') douguo_item['cook_story'] = i.get('r').get('cookstory') douguo_item['image_url'] = i.get('r').get('p') yield douguo_item
結果:
怎樣用scrapy架構下載下傳圖檔
在前面的代碼基礎上繼續更加功能
- Local/Scrapy/douguo/douguo/settings.py 設定圖檔下載下傳路徑,設定下載下傳延遲,激活相應的圖檔下載下傳管道,調整相應優先級
- 路徑IMAGES_STORE是固定寫法,後面的DOWNLOAD_DELAT也是,setting裡的都是如此
- “.”表示目前路徑,屬于相對路徑的寫法,也可以寫成絕對路徑
- 管道優先級,數字越小,優先級越高
# Configure item pipelines # See https://doc.scrapy.org/en/latest/topics/item-pipeline.html ITEM_PIPELINES = { 'douguo.pipelines.DouguoPipeline': 229, 'douguo.pipelines.ImagePipline': 300, } ............. ............. ............. IMAGES_STORE = './images/' DOWNLOAD_DELAY = 1
- Local/Scrapy/douguo/douguo/items.py 設定相應的圖檔下載下傳連結和下載下傳路徑
import scrapy class DouguoItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() auth = scrapy.Field() cook_name = scrapy.Field() cook_time = scrapy.Field() cook_difficulty = scrapy.Field() cook_story = scrapy.Field() image_url = scrapy.Field() image_path = scrapy.Field()
- Local/Scrapy/douguo/douguo/pipelines.py 設定圖檔下載下傳管
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import os import scrapy from scrapy.pipelines.images import ImagesPipeline from scrapy.exceptions import DropItem from .settings import IMAGES_STORE class DouguoPipeline(object): def process_item(self, item, spider): print(item) return item class ImagePipline(ImagesPipeline): def get_media_requests(self, item, info): ''' 對圖檔的位址生成Request請求進行下載下傳 ''' yield scrapy.Request(url=item['image_url']) def item_completed(self, results, item, info): ''' 當圖檔下載下傳完成之後,調用方法 ''' format = '.' + item['image_url'].split('.')[-1] # 設定圖檔格式 image_path = [x['path'] for ok, x in results if ok] # 擷取圖檔的相對路徑 old_path = IMAGES_STORE + image_path[0] # 老的路徑 new_path = IMAGES_STORE + item['cook_name'] + format # 新的路徑 路徑+菜名+格式 item['image_path'] = new_path # 把新的路徑傳給item try: os.rename(old_path, new_path) # 改變下載下傳的位置 except: raise DropItem('Image Download Failed') return item
結果:
怎樣用scrapy架構下載下傳圖檔
ImagePipeline:
Scrapy用ImagesPipeline類提供一種友善的方式來下載下傳和存儲圖檔。需要PIL庫支援。
怎樣用scrapy架構去下載下傳鬥魚APP的圖檔?
常見問題
轉載于:https://www.cnblogs.com/pywjh/p/9951655.html