简单Python3爬虫程序(2)进阶:伪装浏览器、超时功能、保存数据
2016-04-07 20:35
176 查看
<span style="font-size:18px;">import urllib.request import http.cookiejar # head: dict of header def makeMyOpener(head = { 'Connection': 'Keep-Alive', 'Accept': 'text/html, application/xhtml+xml, */*', 'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko' }): cj = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) header = [] for key, value in head.items(): elem = (key, value) header.append(elem) opener.addheaders = header return opener def saveFile(data): save_path = 'D:\\output.out' f_obj = open(save_path,'wb') f_obj.write(data) f_obj.close() oper = makeMyOpener() uop = oper.open('http://www.baidu.com/', timeout = 1000) data1 = uop.read() #.decode('utf-8') saveFile(data1) print(data1)</span>
相关文章推荐
- python 函数式编程 一些高阶函数的使用
- Django学习笔记 Django的工程目录
- 分享一个写python特别好的个人的博客
- Feather R语言和Python交互式硬盘存储格式
- 简单Python3爬虫程序(1)简单架构:队列、集合、正则
- Python的getattr(),setattr(),delattr(),hasattr()
- Django学习笔记 开发环境搭建
- python reduce函数
- CNKI小爬虫(Python)
- ubuntu下Eclipse创建Django项目
- PyQt QString转成python stirng
- python fabric实现远程操作和部署
- 代码迁移攻略:在Java工程中调用Python代码
- Python xrange与range
- python-摩尔斯电码查询器
- python GUI学习——Tkinter
- python 学习笔记十二 html基础(进阶篇)
- Implementing a Neural Network from Scratch in Python – An Introduction
- python学习5
- 常见的python错误类型和继承关系