import urllib.request
contents = urllib.request.urlopen(f"http://127.0.0.1:8000/").read()
Python http-запросы
Добавить комментарий
import urllib.request
contents = urllib.request.urlopen(f"http://127.0.0.1:8000/").read()
Скрипт читает в переменную содержимое html-страницы:
# -*- coding: utf-8 -*-
import http.client
import ssl
print('Program started')
#выполнить http-запрос
print('Sending request to site...')
#https://stackoverflow.com/questions/39945702/httplib-httpsconnection-issue-certificate-verify-failed#39945733
conn = http.client.HTTPSConnection('kmsvsr.ru', timeout=5, context=ssl._create_unverified_context())
conn.request("GET", "/")
resp = conn.getresponse()
print(resp.status, resp.reason)
print('Reading answer...')
data = resp.read().decode()
print (data)
Скрипт не дописан, кто будет пользоваться — не забудьте добавить сюда обработку ошибок. А так по быстрому спарсить страницу пойдёт.
Из обобенностей: здесь в примере открывается сайт не по HTTP, а по HTTPS, при чём включен игнор ошибок, то есть код можно использовать для парсинга сайтов с самоподписанными сертификатами.