python-网络编程urllib模块

一、操作网络发送请求

from urllib.request import urlopen   #发送请求

from urllib.parse import urlencode   #用来把字典形式转换成k=v形式;username = dusir&pwd = 123456

####get请求

url='http://127.0.0.1:/8000/login'

url1='http://127.0.0.1:/8000/login?usernanme = gongzai&pwd = 1234556'

res = urlopen(url+'?'usernanme = gongzai&pwd = 1234556').read().decode()   

res = urlopen(url)1.read()decode()

# 请求默认返回的是bytes类型的所以要decode成str类型

print(res)

####pos请求

data ={"user_id":1,"price":"999"}

res = urlopen(url,data.encode()).read().decode()   #需要先转成bytes在转成str

param = urlencode(data) 是把字典转成k=v形式username=jmy1235&price=123456

二、编码解码

from urllib.parse import quote,unquote #  用来做url编码和解码

eg.1:

url = "http://www.fengniao.com/search.php?keyword=%C3%C0%C5%AE&charset=gbk"

print(unquote(url))    #解码

s = 'kkkk%ddt%ff'

print(quote(s))  #编码

 

posted @ 2017-11-24 11:50  D.零下的小书屋  阅读(176)  评论(0编辑  收藏  举报