Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Voko
Created November 30, 2021 23:47
Show Gist options
  • Save Aleksey-Voko/827a029380c71aaf18b3c28f58f1124b to your computer and use it in GitHub Desktop.
Save Aleksey-Voko/827a029380c71aaf18b3c28f58f1124b to your computer and use it in GitHub Desktop.
Get and save html
"""
Get and save html.
============
"""
import requests
USER_AGENT = ('Mozilla/5.0 (iPhone; CPU iPhone OS 14_7 like Mac OS X) '
'AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/92.0.4515.90 '
'Mobile/15E148 Safari/604.1')
HEADERS = {'User-Agent': USER_AGENT}
def get_html(url):
response = requests.get(url, headers=HEADERS)
print(url)
print(f'status code = {response.status_code}')
print('=' * 30)
print()
return response.text
def save_html(url, out_f, encoding='utf-8'):
html_code = get_html(url)
with open(out_f, 'w', encoding=encoding) as fl:
fl.write(html_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment