python3 urllib post操作

类别:信息技术 作者:ATScore 发布日期:2022年1月14日

使用urllib提交http请求

访问url,读取数据

import urllib
import urllib.request
try:
    url =  "https://baidu.com"
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0'}
    req = urllib.request.Request(url=url, headers=headers)
    file = urllib.request.urlopen(req)
    data = file.read()
    file.close
except urllib.request.URLError as e:
    print(e)

post提交,解析结果

query = {"q": "python"}
url = "http://DEST_URL"
post_data = urllib.parse.urlencode(query).encode('utf-8')
headers = {
    "POST": url,
    "Host": get_host(url),  #自己实现get_host
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0",
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate",  #压缩方式,自己先研究
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "X-Requested-With": "XMLHttpRequest",
    "Origin": get_origin(url), #自己实现地址,类似get_host
    "Connection": "keep-alive",
    "Referer": get_src_page(url),  #写跳转源页面
    "Cookie": g_cookie   #关键,自己找到cookie
}

try:
    req = urllib.request.Request(url=url, data=post_data, headers=headers)
    rsp = urllib.request.urlopen(req)
    data = rsp.read()
    rsp.close()

    data = gzip.decompress(data).decode('utf-8') #如果是gzip压缩

    d = json.loads(data)
except Exception as e:
    print(e)