1. 网页抓取

    # -*-coding: utf-8 -*-
    
    import urllib
    
    url = "http://www.cndzz.com/"
    
    html = urllib.urlopen(url)
    
    print html.read()
    

      对于网页编码为gb2312等格式的网页,使用如下方法

    # -*-coding: utf-8 -*-
    
    import urllib
    
    url = "http://www.sina.com.cn/"
    
    html = urllib.urlopen(url)
    
    print html.read().decode("gbk").encode("utf-8")
    

      如果有多种编码,可以使用如下方法

    # -*-coding: utf-8 -*-
    # Author:Evilxr import urllib url = "http://www.sina.com.cn/" html = urllib.urlopen(url) print html.read().decode("gbk", "ignore").encode("utf-8")

      

  2. 获取Web服务器头部信息
    # -*-coding: utf-8 -*-
    # Author:Evilxr import urllib url = "http://www.sina.com.cn/" html = urllib.urlopen(url) print html.info()

      返回信息:

    Server: nginx
    Date: Otc, 10 Nov 2014 12:54:50 GMT
    Content-Type: text/html
    Last-Modified: Otc, 10 Nov 2014 12:54:11 GMT
    Vary: Accept-Encoding
    Expires: Otc, 10 Nov 2014 12:55:50 GMT
    Cache-Control: max-age=60
    X-Powered-By: schi_v1.03
    Age: 27
    Content-Length: 563513
    X-Cache: HIT from cd31-151.sina.com.cn
    Connection: close [Finished in 0.2s]

      

  3. 获取网页状态码
    # -*-coding: utf-8 -*-
    # Author:Evilxr import urllib url = "http://www.sina.com.cn/" html = urllib.urlopen(url) # 200正常访问 301重定向 403 禁止访问 404页面不存在 500 服务器忙或者服务器无响应
    print html.getcode() # 获取用户传入的url
    print html.geturl() # 关闭文件
    html.close

      

  4. 保存网页内容
    # -*-coding: utf-8 -*-
    # Author:Evilxr import urllib url = "http://www.cdnzz.com/" urllib.urlretrieve(url, "d:\\evilxr.html")

      

  5. 获取网站编码类型
    # coding:utf8
    # Author:Evilxr import urllib url = "http://www.163.com" html = urllib.urlopen(url) print html.info().getparam('charset')
    html.close()

      返回:

    GBK
    [Finished in 0.6s]

      

    # coding:utf8
    # Author:Evilxr import urllib url = "http://www.cnblogs.com/Evilxr" html = urllib.urlopen(url) print html.info().getparam('charset')
    html.close()

      返回:

    utf-8
    [Finished in 0.3s]

      

  6. 自动获取网站编码 chardet[字符集检测]
    #先安装chardet
    #pip install chardet
    # coding:utf8
    
    import urllib
    import chardet def automatic_detect(url):
    """" doc """
    content = urllib.urlopen(url).read()
    result= chardet.detect(content)
    encoding = result['encoding']
    return encoding url_list = ["http://www.sina.com.cn/",
    "http://www.cnblogs.com/evilxr",
    "http://bbs.hackav.com/",
    "http://www.baidu.com/",
    "http://fuli.ba/"]
    for url in url_list:
    print url, automatic_detect(url)
    http://www.sina.com.cn/ GB2312
    http://www.cnblogs.com/evilxr utf-8
    http://bbs.hackav.com/ GB2312
    http://www.baidu.com/ utf-8
    http://fuli.ba/ utf-8
    [Finished in 17.1s]

      

Python 爬虫学习 urllib的更多相关文章

  1. python爬虫学习(1) —— 从urllib说起

    0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...

  2. python爬虫学习 —— 总目录

    开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...

  3. Python爬虫学习:三、爬虫的基本操作流程

    本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...

  4. 《Python爬虫学习系列教程》学习笔记

    http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...

  5. Python爬虫之urllib模块2

    Python爬虫之urllib模块2 本文来自网友投稿 作者:PG-55,一个待毕业待就业的二流大学生. 看了一下上一节的反馈,有些同学认为这个没什么意义,也有的同学觉得太简单,关于Beautiful ...

  6. python爬虫学习笔记(一)——环境配置(windows系统)

    在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库)   [推荐地址:清华镜像] https://mirrors ...

  7. [转]《Python爬虫学习系列教程》

    <Python爬虫学习系列教程>学习笔记 http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多. ...

  8. Python爬虫学习第一记 (翻译小助手)

    1 # Python爬虫学习第一记 8.24 (代码有点小,请放大看吧) 2 3 #实现有道翻译,模块一: $fanyi.py 4 5 import urllib.request 6 import u ...

  9. Python爬虫学习:四、headers和data的获取

    之前在学习爬虫时,偶尔会遇到一些问题是有些网站需要登录后才能爬取内容,有的网站会识别是否是由浏览器发出的请求. 一.headers的获取 就以博客园的首页为例:http://www.cnblogs.c ...

随机推荐

  1. mongoDB 数据导出与导入

    一.导出 命令格式:在mongodb/bin目录下 mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv ...

  2. git@Osc初识

    加油! 参考博客:http://www.cnblogs.com/lpshou/archive/2013/07/18/3199243.html 今天尝试了下git@osc的项目导入,基本算是入门了git ...

  3. [css]样式合并与模块化

    原文链接:http://www.zhangxinxu.com/wordpress/2010/07/css%E7%9A%84%E6%A0%B7%E5%BC%8F%E5%90%88%E5%B9%B6%E4 ...

  4. Error: listen EADDRINUSE

    有可能是已经作用了18001端口 netstat -antp|grep 18001 kill 然后重启程序. events.js:72 throw er; // Unhandled 'error' e ...

  5. Spring IoC原理详解

    去掌握一门技术的时候,往往很多人都忽略了一点,只是一味地去写代码,原理层面的东西从来就不理会 还有就是学习的过程中,不去想为什么有了当前的写法,却有着这么一门技术可以代替它 一般来说,在写程序的时候, ...

  6. github文件上传及github pages博客搭建教程

    一.与github建立连接 1.安装node.js和git 2.桌面新建文件夹[github],右键“git bash here” 3.注册github账号,新建仓库“new repository”, ...

  7. 1057 N的阶乘(大数运算)

    题目链接:51nod 1057 N的阶乘 #include<cstdio> using namespace std; typedef long long ll; ; const int m ...

  8. 第十二章 非对称加密算法-RSA

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 12.1.RSA(最经典的非对称加密算法) 特点: 使用一套密钥即可完成加解密(与D ...

  9. mongostat 3.2指标详解

    存储引擎:wiredTiger /usr/local/mongodb-3.2.8/bin/mongostat  -uroot -pcEqHuoqiJYhjVpuL --host 127.0.0.1   ...

  10. 安装Pod时提示ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/pod

    环境:OSX EI 10.11.1 昨天切换gem源后,招待pod安装没有任何问题,也可以正常用$ gem sources --add https://ruby.taobao.org/ --remov ...