# *-* encoding:utf-8 -*-
#
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com',data=data)
print(response.read().decode())
data 参数
import urllib.request,urllib.parse
data = bytes(urllib.parse.urlencode({'wd':'hello'}),encoding='utf-8')
response = urllib.request.urlopen('https://www.***.com/s?', data=data,timeout=1)
print(response.read().decode())
header参数
import urllib.request
url ='http://www.baidu.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
}
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request).read().decode()
print(response)
This function always returns an object which can work as a context manager and has methods such as
geturl()
— return the URL of the resource retrieved, commonly used to determine if a redirect was followedinfo()
— return the meta-information of the page, such as headers, in the form of anemail.message_from_string()
instance (see Quick Reference to HTTP Headers)getcode()
– return the HTTP status code of the response.
import urllib.request
url ='https://blog.csdn.net/duxu24/article/details/77414298'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
}
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
# print(response.read().decode())
print(response.getcode())
print(response.info())
print(response.geturl())