2016-08-01 8 views
6

私はこのページからChromeのデバッグプロトコルを使用してWebページのリソースコンテンツを使用したいと考えていますmethod-getResourceContent、このメソッドに気付きました:getResourceContent、params frameIdとurl.iが必要です私は欲しい。 。\ chrome.exe --remote-デバッグポート= 9222クロムリモートデバッグを使用してウェブページのリソースコンテンツを取得する方法

2.write Pythonのテストコード:

# coding=utf-8 
""" 
chrome --remote-debugging api test 
""" 

import json 
import requests 
import websocket 

import pdb 

def send(): 
    geturl = requests.get('http://localhost:9222/json') 
    websocketURL = json.loads(geturl.content)[0]['webSocketDebuggerUrl'] 
    request = {} 
    request['id'] = 1 
    request['method'] = 'Page.navigate' 
    request['params'] = {"url": 'http://global.bing.com'} 
    ws = websocket.create_connection(websocketURL) 
    ws.send(json.dumps(request)) 
    res = ws.recv() 
    ws.close() 
    print res 

    frameId = json.loads(res)['result']['frameId'] 
    print frameId 
    geturl = requests.get('http://localhost:9222/json') 
    websocketURL = json.loads(geturl.content)[0]['webSocketDebuggerUrl'] 
    req = {} 
    req['id'] = 1 
    req['method'] = 'Page.getResourceContent' 
    req['params'] = {"frameId":frameId,"url": 'http://global.bing.com'} 
    header = ["User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"] 
    pdb.set_trace() 
    ws = websocket.create_connection(websocketURL,header=header) 
    ws.send(json.dumps(req)) 
    ress = ws.recv() 
    ws.close() 
    print ress 
if __name__ == '__main__': 
    send() 
サーバーとして

し1.Get開始クローム: ので、私はこの事をしました :

3.Page.navigate作業罰金、私はこのような何かだ{ "ID":1、 "結果":{ "frameIDの": "8504.2"}}

たら、4.私はこの方法を試してください。getResourceContentをエラーが発生しました: {"error":{"code": - 32000、 "message": "エージェントが有効になっていません"}、" id ":1}

私はUser-Agentを追加しようとしましたが、まだ動作しません。

ありがとうございました。

答えて

2

"エージェントは有効になっていません"というエラーメッセージは、HTTP User-Agentヘッダーとは関係ありませんが、ページコンテンツを取得するために有効にする必要があるクロム内のエージェントを示します。 protocol documentationがそれらをデバッグするために有効にする必要があるドメインについて語っているので

用語「薬剤」は少し誤解を招くおそれがあり

を(用語「薬剤」は、これは内部的にChromeで実装される方法を意味し、私は考えます)

ページコンテンツにアクセスするには、どのドメインを有効にする必要があるのですか?後見では明らかです。Pageドメインを有効にして、このドメインでメソッドを呼び出す必要があります。私はthis exampleをつまずいた後でこれを見つけました。

ドメインを有効にするスクリプトにリクエストを追加すると、エラーメッセージが表示されなくなりました。 HTTPへ移動するとき

  1. のWebSocket接続はクロームが呼び出し間でいくつかの状態を保つよう要求間で開いたままにする必要があります(たとえば、エージェントが有効になっているかどうかなど)
  2. :しかし、私は2つの他の問題に遭遇しました/ /global.bing.com/ブラウザはhttp://www.bing.com/(少なくとも私のコンピュータ上にあります)にリダイレクトされています。これにより、Page.getResourceContentは、要求されたリソースhttp://global.bing.com/が利用できないため、リソースの取得に失敗します。

これらの問題を修正した後、ページのコンテンツを取得できました。これは私のコードです:

# coding=utf-8 
""" 
chrome --remote-debugging api test 
""" 

import json 
import requests 
import websocket 

def send(): 
    # Setup websocket connection: 
    geturl = requests.get('http://localhost:9222/json') 
    websocketURL = json.loads(geturl.content)[0]['webSocketDebuggerUrl'] 
    ws = websocket.create_connection(websocketURL) 

    # Navigate to global.bing.com: 
    request = {} 
    request['id'] = 1 
    request['method'] = 'Page.navigate' 
    request['params'] = {"url": 'http://global.bing.com'} 
    ws.send(json.dumps(request)) 
    result = ws.recv() 
    print "Page.navigate: ", result 
    frameId = json.loads(result)['result']['frameId'] 

    # Enable page agent: 
    request = {} 
    request['id'] = 1 
    request['method'] = 'Page.enable' 
    request['params'] = {} 
    ws.send(json.dumps(request)) 
    print 'Page.enable: ', ws.recv() 

    # Retrieve resource contents: 
    request = {} 
    request['id'] = 1 
    request['method'] = 'Page.getResourceContent' 
    request['params'] = {"frameId": frameId, "url": 'http://www.bing.com'} 
    ws.send(json.dumps(request)) 
    result = ws.recv() 
    print("Page.getResourceContent: ", result) 

    # Close websocket connection 
    ws.close() 

if __name__ == '__main__': 
    send() 
関連する問題