urllib.request.urlopen
from utllib import request
resp = request.urlopen('http://www.baidu.com')
print(resp.read())
import ssl
from urllib import request
import json
if __name__ == '__main__':
context = ssl._create_unverified_context()
url = r'https://api.lab.wayougou.com/api/stocks/rank'
response = request.urlopen(url, context=context)
html = json.loads(response.read())
print(type(html))
print(html)
print()
[print(i) for i in html]
返回值: read(size), readline, readlines, getcode
下载页面urlretrieve
from urllib import request
request.urlretrieve('http://www.baidu.com/', './baidu.html')
request.urlretrieve('xxxx.img', './luban.jpg')
编码urlencode函数
from urllib import request
from urllib import parse
params = {'name': '哈哈', 'age': 18, 'greet':'hello world'}
result = parse.urlencode(params)
print(result)
用程序的方式请求百度搜索刘德华https://www.baidu.com/s?wd=刘德华
—>https://www.baidu.com/s?wd=%E5%88%98%E5%BE%B7%E5%8D%8E
这个网址不能直接被urlopen打开, 因为urlopen只解析ascii
from urllib import request
from urllib import parse
url = 'https://www.baidu.com/s'
params = {'wd': '刘德华'}
qs = parse.urlencode(params)
url = url + '?' + qs
resp = request.urlopen(url)
print(resp.read())
parse_qs 解码
params = {'name': '哈哈', 'age': 18, 'greet':'hello world'}
qs = parse.urlencode(params)
print(qs)
result = parse.parse_qs(qs)
print(result)