urllib
包含模块
urllib.request:打开和读取urls
urllib.error:包含urllib.request产生的常见的错误,使用try捕捉
urllib.parse:包编码url的方法
案例1
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#__author__ = DrYao
from urllib import request
if __name__ == '__main__':
url = "https://blog.youkuaiyun.com/csdn_am/article/details/79924744"
rsp = request.urlopen(url)
html = rsp.read()
print(type(html))
html = html.decode()
print(html)
- 网页编码问题解决
- chardet 可以自动检测页面文件的编码格式,但是可能有错误
- 需要安装,conda install chardet
案例V2
if __name__ == '__main__':
url = "https://blog.youkuaiyun.com/csdn_am/article/details/79924744"
rsp = request.urlopen(url)
html = rsp.read()
cs = chardet.detect(html)
print(type(cs))
html = html.decode(cs.get("encoding","utf-8"))
print(html)
-
URLOPEn的返回对象
- geturl:返回请求对象的URL
- info:请求返回对象的meta信息
- getcode:返回的http code
案例V3
from urllib import request
import chardet
if __name__ == '__main__':
url = "https://blog.youkuaiyun.com/csdn_am/article/details/79924744"
rsp = request.urlopen(url)
print("Url:%s"%(rsp.geturl()))
print("info:%s"%(rsp.info()))
print("Code:%s"%(rsp.getcode()))from urllib import request
import chardet
if __name__ == '__main__':
url = "https://blog.youkuaiyun.com/csdn_am/article/details/79924744"
rsp = request.urlopen(url)
print("Url:%s"%(rsp.geturl()))
print("info:%s"%(rsp.info()))
print("Code:%s"%(rsp.getcode()))
- request.date 的使用
- 访问网络的两种方法
- get:
- 利用参数给服务器传递信息,
- 参数为dict,然后用parse编码
- post:
- 一般用来向服务器传递参数使用
- post是把信息自动加密处理
- 我们如果想使用post信息,需要用到data参数
- 使用post,意味着Http的请求可能需要更改:
- - conntent_type:application/x_www.from-urlencode
- - Content - Length :数据长度
- - 简而言之,一旦更改请求方法,请注意其他请求头部信息相适应
- urllib.parse.urlencode可以将字符串转换成服务器需要的输入值
案例V4
from urllib import request,parse
if __name__ == '__main__':
url = "https://www.baidu.com/s?"
wd = input("Input your keyword:")
rsp = request.urlopen(url)
qs = {"wd":wd}
qs = parse.urlencode(qs)
print(qs)
rsp = request.urlopen(url)
fullurl = url + qs
print(fullurl)
html = rsp.read()
html = html.decode()
print(html)
#parse模块
利用parse模块模拟post请求
分析百度词典
分析步骤:
1.打开F12
2.尝试输入单词GIRL,发现每一个字母后面都有请求
3.请求地址是https://fanyi.baidu.com/sug
4.利用NETwork_all-Hearders查看,发现FormData的值是kw:girl
案例V5
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#__author__ = DrYao
from urllib import request,parse
import json
baseurl = 'https://fanyi.baidu.com/sug'
data = {'kw':'man'}#girl是翻译输入的英文内容,应该由用户输入,此处使用硬编码
#需要使用parse模块对data进行编码
data = parse.urlencode(data).encode()
#我们需要构造一个请求头,请求头部应该至少包含传入的数据的长度
#request要求传入的请求头是一个dict格式
headers = {
#因为使用post,至少应该包含content_length字段
'Content-Length':len(data)
}
rsp = request.urlopen(baseurl, data=data)
json_data = rsp.read().decode('utf-8')
print(json_data)
#把jason字符串转化成字典
json_data = json.loads(json_data)
print(json_data)
for item in json_data['data']:
print(item["k"],"--",item['v'])
- 为了更多的设置请求信息,单纯的通过urlopen函数已经不太好用了
- 需要利用request.Request类
- 案例6
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#__author__ = DrYao
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#__author__ = DrYao
from urllib import request,parse
import json
baseurl = 'https://fanyi.baidu.com/sug'
data = {'kw':'man'}#girl是翻译输入的英文内容,应该由用户输入,此处使用硬编码
#需要使用parse模块对data进行编码
data = parse.urlencode(data).encode()
#我们需要构造一个请求头,请求头部应该至少包含传入的数据的长度
#request要求传入的请求头是一个dict格式
headers = {
#因为使用post,至少应该包含content_length字段
'Content-Length':len(data)
}
#构造一个Request的实例
req = request.Request(url = baseurl,data=data,headers=headers)
rsp = request.urlopen(req)
json_data = rsp.read().decode('utf-8')
print(json_data)
#把jason字符串转化成字典
json_data = json.loads(json_data)
print(json_data)
for item in json_data['data']:
print(item["k"],"--",item['v'])