-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_request.py
31 lines (28 loc) · 908 Bytes
/
web_request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Network requests with library
import requests as requestlib
import urllib
import json
HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"}
try:
r = requestlib.get("https://jsonplaceholder.typicode.com/users/10")
print(r.status_code)
print("json:", r.json())
except (requestlib.HTTPError, Exception) as e:
print("ERROR:", repr(e))
#Request with urlib
print("""
Using urlib
""")
try:
data = {}
req = urllib.request.Request(url="https://jsonplaceholder.typicode.com/users/10",
headers=HEADERS, method="GET")
with urllib.request.urlopen(req) as response:
response_text = response.read()
print(response.status)
data = json.loads(response_text)
if not data:
print("NO user")
print("URLIBDATA:", data)
except (urllib.error.HTTPError, Exception ) as e:
print("ERROR:", repr(e))