使用python 3的urllib.request库

时间:2022-10-08 18:12:14
普通GET请求
#普通GET请求
import urllib.request

url="http://www.hao123.com/"
htmlObj = urllib.request.urlopen(url)
html = htmlObj.readline().decode("UTF-8")
html = htmlObj.info()
html = htmlObj.getcode()
html = htmlObj.geturl()
html = htmlObj.read().decode("UTF-8")
print(html)


带参数的简单POST示例

# 带参数的简单POST示例
import urllib.request
import urllib.parse

url="http://www.hao123.com/"
postData={"性别":"男"}
postDataEncode = urllib.parse.urlencode(postData).encode(encoding="UTF8")
#关键一句
htmlObj = urllib.request.urlopen(url,data = postDataEncode)
html = htmlObj.read().decode("UTF-8")
print(html)


带头的POST示例

# 带头的POST示例
import urllib.request
import urllib.parse

url="http://www.hao123.com/"
postData={"性别":"男"}
postDataEncode = urllib.parse.urlencode(postData).encode(encoding="UTF8")
#关键两句
request = urllib.request.Request(url,data = postDataEncode)
request.add_header("Referer",url)
htmlObj = urllib.request.urlopen(request)
html = htmlObj.read().decode("UTF-8")
print(html)


#SSL证书问题(HTTPS)

#SSL证书问题(HTTPS)
import ssl
ssl._create_default_https_context = ssl._create_unverified_context