Python3 urllib库爬虫 基础
来源:互联网 发布:康乾盛世知乎 编辑:程序博客网 时间:2024/05/22 08:17
基础
add_header()添加报头
url="http://blog.csdn.net/yudiyanwang/article/details/78322039"req = urllib.request.Request(url)req.add_header("User-Agent","Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0")data = urllib.request.urlopen(req).read()print(data)
GET请求
keyword = "hello" #要搜索的关键字url = "http://www.baidu.com/s?wd=hello"req = urllib.request.Request(url)data = urllib.request.urlopen(req).read()with open("./result.txt","wb") as fd: fd.write(data)# 上述当检索中文的时候 编码错误 keyword = "你好"key_code = urllib.request.quote(keyword) # 编码url = "http://www.baidu.com/s?wd=" + key_codeprint(url) # http://www.baidu.com/s?wd=%E4%BD%A0%E5%A5%BDreq = urllib.request.Request(url)data = urllib.request.urlopen(req).read()with open("./result.txt","wb") as fd: fd.write(data)
post请求
# PHP页面<form action="" method="post"> <input name="name" type="text" /></br> <input name="pass" type="text" /><br> <input name="" type="submit" value="submit"/></form>#请求地址url = "http://192.168.1.108/login.html"# 构建表单数据并进行编码处理postdata = urllib.parse.urlencode({ "name":"abcdef", "pass":"123456"}).encode("utf-8")# 创建Request对象 参数包括URL地址和要传递的数据req =urllib.request.Request(url,postdata)# 添加头信息req.add_header("User-Agent","Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0")data =urllib.request.urlopen(req).read()with open("./post.txt",'wb') as fd: fd.write(data)
一边运行 一边打印日志 开启DebugLog
httpd = urllib.request.HTTPHandler(debuglevel=1)httpsd = urllib.request.HTTPSHandler(debuglevel=1)opener = urllib.request.build_opener(httpd,httpsd)urllib.request.install_opener(opener)data = urllib.request.urlopen("http://edu.jd.com")
异常
# URLerror异常 1,连接不上远程服务器,2,远程URL不存在,3 无网络,4 触发了HTTPErrortry: data = urllib.request.urlopen("http://blog.csdn1.net").read() print(data)except urllib.error.URLError as e: # print(e.code) # print("-----------------------") print(e.reason)# 当构造一个存在的网址,引发的异常不能用HTTPError处理,要用URLError处理 ,URLError是HTTPError的父类
阅读全文
0 0
- Python3 urllib库爬虫 基础
- python3 urllib 爬虫乱码问题解决
- python3 urllib爬虫抓取记录
- Python3爬虫学习笔记(1.urllib库详解)
- python3 标准库 urllib
- Python3网络爬虫(三):urllib.error异常
- python3使用urllib模块制作网络爬虫
- Python3.5爬虫urllib系列之三
- python3+urllib撸新浪滚动新闻爬虫
- python2和python3中urllib的用版本区别及用法 爬虫基础
- Python爬虫-urllib库
- Python的Urllib库的使用(爬虫基础)
- Python3爬虫学习笔记1.1——urllib官方库的使用
- Python3 爬虫学习(一):urllib库的使用及简单的爬取
- 【笔记】3、初学python3网络爬虫——urllib库的使用
- python3爬虫基础
- python3爬虫基础学习
- Python3 爬虫基础
- spark连接mongodb(权限认证)示例
- P3479【2015多校联训2】最短路径(多维dp)
- 双向链表的插入和删除
- 安卓开发之SharedPreferences的工具类分享(包括保存复杂对象)
- CCF 学生排队 (JAVA) -20170302
- Python3 urllib库爬虫 基础
- go-ethereum 搭建本地测试私有链
- 在Linux下安装Node.js
- MYSQL一次千万级连表查询优化(一)
- python 通过pyhs2进行hql,报错
- 【codevs 2178】表达式运算Cuties
- thinkphp ajax无刷新分页类及实例
- kafka和spark集成启动报错java.lang.NoClassDefFoundError:org/apache/zookeeper/Watcher
- Python中函数定义及基本操作