Skip to content

Instantly share code, notes, and snippets.

@gwpl
Created July 26, 2021 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwpl/cea937962d1f1c0f9a87567b6d58d92f to your computer and use it in GitHub Desktop.
Save gwpl/cea937962d1f1c0f9a87567b6d58d92f to your computer and use it in GitHub Desktop.
pychrome scraping fetch images Chrome DevTools Protocol Viewer
import base64
import pychrome
# NOT WORKING, copied FTR as copy for "later" from:
# from https://stackoverflow.com/a/61311648/544721
def save_image(file_content, file_name):
try:
file_content=base64.b64decode(file_content)
with open("C:\\Crawler\\temp\\" + file_name,"wb") as f:
f.write(file_content)
except Exception as e:
print(str(e))
def response_received(requestId, loaderId, timestamp, type, response, frameId):
if type == 'Image':
url = response.get('url')
print(f"Image loaded: {url}")
response_body = tab.Network.getResponseBody(requestId=requestId)
file_name = url.split('/')[-1].split('?')[0]
if file_name:
save_image(response_body['body'], file_name)
tab.Network.responseReceived = response_received
# start the tab
tab.start()
# call method
tab.Network.enable()
# get request to target the site selenium
driver.get("https://www.realtor.com/ads/forsale/TMAI112283AAAA")
# wait for loading
tab.wait(50)
@gwpl
Copy link
Author

gwpl commented Jul 26, 2021

tab.call_method("Network.enable")
tab.call_method("Page.navigate", url="https://github.com/fate0/pychrome", _timeout=5)

from https://fate0.github.io/pychrome/#examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment