python中urllib库的用法

2019-09-29  本文已影响0人  自序

urllib

urlopen()函数

from urllib import request
resp = request.urlopen("http://www.httpbin.org")
print(resp.read())
print(resp.getcode()) #获取返回状态吗
print(resp.readline())#逐行读取


urlretrieve函数用法

request.urlretrieve("http://www.httpbin.org",'tset.txt')#下载函数

urlencode用法

###编码
from urllib import parse

params = {
    "name":"张三",
    'age':18,
    'greet':'helloworld'
}
qs = parse.urlencode(params)
print(qs)


from urllib import parse
params = {'name':'张三','age':'18','greet':'hello   world'}
result = parse.urlencode(params)
print(result)

url ="https://www.baidu.com/s"
paramss = {'wd':"刘德华"}
qs = parse.urlencode(paramss)
url = url+"?"+qs
resp = request.urlopen(url)
print(resp.read().decode("utf-8"))

Parse_qs用法

from urllib import parse

params = {
    "name":"张三",
    'age':18,
    'greet':'helloworld'
}
qs = parse.urlencode(params) #编码
print(qs)

result = parse.parse_qs(qs)#解码
print(result)

urlparse

urlparse模块主要是用于解析url中的参数 对url按照一定格式进行 拆分或拼接

将url分为6个部分,返回一个包含6个字符串项目的元组:协议、位置、路径、参数、查询、片段。

from urllib import parse

url = "https://www.baidu.com/baidu?isource=infinity&iname=baidu&itype=web&tn=02003390_42_hao_pg&ie=utf-8&wd=mac%20markdown"
result = parse.urlparse(url)
print(result)
print(result.scheme) 
print(result.netloc)
print(result.path)
print(result.query)
result_dict = parse.parse_qs(result.query)

for i in result_dict.items():
    print(i)
上一篇下一篇

猜你喜欢

热点阅读