python3之urllib基础

urllib简单应用
html=urllib.request.urlopen(域名/网址).read().decode('utf-8')----->
--->urlopen-->获取源码
--->read()-->读取内容
--->decode('utf-8')-->将字节转换为字符串
页面的具体内容可以用正则获取

os+urllib:将网络文件下载到本地
file_path=os.path.join(os.getcwd() + '/name.html')
urllib.request.urlretrieve('域名',file_path)
urllib.requesy.urlcleanup()-->清理内存

部分函数
令 a=urllib.request.urlopen('域名')
a.info()-->获取页面简介
a.getcode()-->获取状态码
a.geturl()-->获取当前页面url

超时问题
html=urllib.request.urlopen("域名",timeout=30)-->超过对应时间则不抓取

get

quote()将关键词转码成浏览器认识的字符,默认网站不能是中文

post请求

urlencode()封装post请求提交的表单数据,参数是字典形式的键值对表单数据
Request()提交post请求,参数1是url地址,参数2是封装的表单数据

原文地址:https://www.cnblogs.com/woshiruge/p/8256867.html