重新认识urllib
2024-10-08 09:12:47
# coding=utf-8
# urllib_get_file=urllib.request.urlretrieve(url=None,filename="test.zip")
# basic usage of urllib
from urllib import request
url = "https://www.cnblogs.com/SunshineKimi/"
msg = request.Request(url)
# msg.add_header("1",2)
# msg.set_proxy()
rep = request.urlopen(msg)
print(rep.read()) # how to use proxy to request in urllib
proxies = {"http": "user:passwd@ip:port"} # buy proxy
proxy = {"http": "111.79.44.217:9999"} # free proxy
proxy_handler = request.ProxyHandler(proxy) # there also exist HttpHandler() basic handler
opener = request.build_opener(proxy_handler)
response = opener.open(url, data=None)
print(response.headers) # auth by proxy
passwd_manager=request.HTTPPasswordMgrWithDefaultRealm()
passwd_manager.add_password(realm=None,uri=proxy,user="user",passwd="password")
buy_auth_handler=request.ProxyBasicAuthHandler(passwd_manager)
opener_auth=request.build_opener(buy_auth_handler)
response_auth=opener_auth.open(url=None,data=None).read()
print(response_auth)
最新文章
- .Net程序员之Python基础教程学习----判断条件与循环[Fourth Day]
- UITableView和UICollectionView的方法学习一
- Android 2.3 不支持印度文
- [转载]HTML5 Audio/Video 标签,属性,方法,事件汇总
- HTML几类标签的应用总结
- [Thinkbayes]贝叶斯思维读书笔记-2-火车头问题
- print打印网页相关
- perl的正则表达式
- deeplearning.ai 改善深层神经网络 week3 超参数调试、Batch正则化和程序框架 听课笔记
- Python之路【第一篇】:Python简介和入门
- I/O模型系列之三:IO通信模型BIO NIO AIO
- SVN忽略已提交的文件(ignore,移出版本控制)
- 《了不起的 nodejs》中 TwitterWeb 案例 bug 解决
- CF1109D Sasha and Interesting Fact from Graph Theory
- .NET Core微服务之路:基于Consul最少集群实现服务的注册与发现(二)
- HDU 4391 Paint The Wall(分块的区间维护)
- (转发)一个通用的C++ 消息总线框架
- 深度学习基础系列(七)| Batch Normalization
- go练习4--json 序列号反序列化
- ajax 测试