I want to get the contents of a resource web page using python through Chrome debugging protocol to this page method-getResourceContent , I noticed this method: getResourceContent, need params frameId and url.i believe that this method is necessary to me. so I did the following:
1.get start chrome as server:. \ Chrome.exe --remote-debugging-port = 9222
2.Paste python test code:
# 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()
3.Page.navigate work fine, I got something like this: {"ID": 1, "result": {"frameId": "8504,2"}}
4. When I try the method: getResourceContent, an error occurs: {"error": {"code": - 32000, "message": "Agent is not turned on." }, "id": 1}
I tried to add User-Agent, still not working.
Thanks.
source share