Python爬虫0.2
来源:互联网 发布:淘宝 618 大促 销售额 编辑:程序博客网 时间:2024/06/11 08:22
【http://blog.csdn.net/pleasecallmewhy/article/details/8924889】 urllib2中的两个重要概念:Openers和Handlers 获取一个URL使用默认opener:通过urlopen。Openers使用处理器handlers,所有的“繁重”工作http://blog.csdn.net/pleasecallmewhy/article/details/8924889
笔记:
urllib2中的两个个方法:infoand geturl
urllib2_test10.py来比较一下原始URL和重定向的链接:
按原文网址输出response = urlopen(req)错误,更换网之后成功。
*geturl()返回获取的真实的URL因为urlopen(或者opener对象使用的)或许会有重定向。获取的URL或许跟请求URL不同。
***************************************************************************
***************************************************************************
************插入两个网址
************1.爬取贴吧图片和文本http://m.blog.csdn.net/qq_24421591/article/details/52596076
************2.抓取新闻并自动生成word文档http://m.blog.csdn.net/xiongyangg/article/details/50610545
*******************************************************************************
urllib2_test11.py来测试一下info的应用:
eg : Basic Authentication 基本验证
- #
-*- coding: utf-8 -*- - import
urllib2 -
- #
创建一个密码管理者 - password_mgr
= urllib2.HTTPPasswordMgrWithDefau ltRealm() -
- #
添加用户名和密码 - #
如果知道 realm, 我们可以使用他代替 ``None``. - top_level_url
= "http://example.com/foo/" - password_mgr.add_password(None,
top_level_url, 'why','1223')# (None, top_level_url, username, password) - handler
= urllib2.HTTPBasicAuthHandler(password_mgr)# 创建了一个新的handler -
- #
创建 "opener" (OpenerDirector 实例) - opener
= urllib2.build_opener(handler) -
- a_url
= 'http://www.baidu.com/' - #
使用 opener 获取一个URL - opener.open(a_url)
-
- #
安装 opener. - #
现在所有调用 urllib2.urlopen 将用我们的 opener. - urllib2.install_opener(opener)
阅读全文
0 0
- Python爬虫0.2
- python爬虫-->爬虫基础
- [爬虫] Python爬虫技巧
- Python爬虫
- python 爬虫
- python 爬虫
- python 爬虫
- python爬虫
- Python爬虫
- Python爬虫
- python 爬虫
- Python爬虫
- python爬虫
- python 爬虫
- python 爬虫
- python爬虫
- python爬虫
- python爬虫
- python 3.x(1)
- SpringMVC返回jsonp的问题
- ubuntu安装机器学习环境
- 强制类型转换运算符的优先级
- Python爬虫0.1
- Python爬虫0.2
- python爬虫0.3
- 断言
- python0.4
- 初学nlp&nn
- 《Machine Learning in&…
- windows下多进程通信,基于共享内存环形队列实现
- 华为2017.8月笔试题
- H5多图片压缩加水印上传