Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the HTML source code? Thanks #15

Open
TimeAshore opened this issue Dec 27, 2018 · 4 comments
Open

How to get the HTML source code? Thanks #15

TimeAshore opened this issue Dec 27, 2018 · 4 comments

Comments

@TimeAshore
Copy link

No description provided.

@marty90
Copy link
Owner

marty90 commented Dec 27, 2018

This is not related to this tool. You should see the documentation of the Chrome DevTools Protocol.
However, you could get the body of a HTTP response using: Network.getResponseBody. You must specify the RequestId to this function. To get a RequestId, use: Network.responseReceived.

Another way is to get the DOM of the page, which is the HTML loaded by the browser, eventually modified by scripts: see the DOM page. Use for example:

chrome.DOM.getDocument(depth=-1)

To get the whole DOM in json format.

@TimeAshore
Copy link
Author

Example:

import PyChromeDevTools

chrome = PyChromeDevTools.ChromeInterface()
chrome.Network.enable()
chrome.Page.enable()

chrome.Page.navigate(url="http://www.jianshu.com")
event, messages = chrome.wait_event("Page.frameStoppedLoading", timeout=60)
value = chrome.wait_event("Network.responseReceived", timeout=60)
print(value)

reqid = value[0]['params']['requestId']
print("reqid: ", reqid)
print(chrome.Network.getResponseBody(reqid))

But tip no parameters:

({'method': 'Network.responseReceived', 'params': {'requestId': '1000029019.143', 'loaderId': ......
reqid: 1000029019.143
Traceback (most recent call last):
  File "/home/ldy/workspace/chromedevtols/demo.py", line 14, in <module>
    print(chrome.Network.getResponseBody(reqid))
TypeError: generic_function() takes 0 positional arguments but 1 was given

And when I use chrome. DOM. GetDocument (the depth = 1), HTML is divided, and how to get the original HTML? like this:

<html>
  <head>
  ...
  </head>
  <body>
  ...
  </body>
</html>

Thanks!

@marty90
Copy link
Owner

marty90 commented Dec 28, 2018

Network.getResponseBody(reqid) is wrong. reqid must not be passed as positional argument but as requestId.
E.g.

Network.getResponseBody(requestId=reqid)

@TimeAshore
Copy link
Author

I got it, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants