Quick actions

cmd+k|ctrl+k

Navigation

Languages

Crawl and Store HTML

Snippet info

Language

Python

Visibility

public

Author

rajivm1991

Created

2023-04-24T04:31:47.631087Z

Updated

2023-04-24T04:31:47.631087Z

import hashlib
import requests
import os


def get_html(url):
    fname = './{}.html'.format(hashlib.sha256(url.encode('utf-8')).hexdigest())
    if not os.path.exists(fname):
        resp = requests.get(url)
        assert resp.status_code == 200
        with open(fname, 'w') as f:
            f.write(resp.text)
        return resp.text
    else:
        with open(fname) as f:
            return f.read()
INFO