
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response)
print(response.read().decode('utf-8'))
import urllib.parse
data = bytes(urllib.parse.urlencode({
'hello':'world'}),encoding='utf-8')
response = urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read().decode('utf-8'))
try:
response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.01)
print(response.read().decode('utf-8'))
except urllib.error.URLError as e:
print("time out!")
response = urllib.request.urlopen('http://httpbin.org/get',timeout=111)
print(response.status)
response = urllib.request.urlopen('http://www.baidu.com')
print(response.getheaders())
print(response.getheader('Bdpagetype'))
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'
}
data = bytes(urllib.parse.urlencode({
'name':'ouou'}),encoding='utf-8')
url = 'http://www.douban.com'
req = urllib.request.Request(url=url,data=data,headers=headers,method='POST')
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))