欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

urllib中的urlopen发送get和post请求

程序员文章站 2022-05-03 21:34:07
...

get请求

from urllib import request

url = 'http://www.baidu.com'

res = request.urlopen(url=url)
# print(res.read())
with open('baidu_index.html','w',encoding='utf-8') as f:
    f.write(res.read().decode('utf-8'))

post请求

import json
from urllib import request
from urllib import parse

url = 'https://fanyi.baidu.com/sug'

data_dic = {
    'kw':'girl'
}

data_parse = parse.urlencode(data_dic)
data_b = data_parse.encode('utf-8')
res = request.urlopen(url=url,data=data_b)
res_str = res.read().decode('utf-8')
print(json.loads(res_str))
相关标签: urllib