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. python打怪之路【第一篇】:99乘法表

    需求:实现99乘法表 代码: #!/usr/bin/env python # -*- coding:utf-8 -*- #author chenjing for i in range(10): for ...

  2. spring+mybatis

    ---恢复内容开始--- 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地 ...

  3. 饮水思源——python中常用基础类源码解析

    1.bool类 2.int类 3.long类 4.float类 5.str类 6.list类 7.tuple类 8.dict类 9.collections类 Counter类:为hashable对象计 ...

  4. win10窗口设置眼睛保护色

    经常电脑前坐着,习惯了豆沙色窗口(据说保护眼睛): 目标 记事本,ide,office等窗口颜色豆沙色:如下图 步骤 打开注册表:win+r 运行"regedit": 依次打开[H ...

  5. 20150207读书笔记<深入理解计算机系统2-1>

    第二章 信息存储 (1)  多数计算机以一个字节作为最小可寻址的存储器单元. 机器级程序将存储器看成一个非常大的字节数组,称为虚拟存储器. 存储器的每个字节都由唯一的数字标识,称为它的地址. 所有可能 ...

  6. node+mongodb+ionic+cordova

    node + mongodb1,环境 windows 1,install nodejs 2,install npm | cd npmjs node cli.js install -gf1.1 2.no ...

  7. linux C++ 共享库导出类

    1.共享库的对外接口函数的声明必须加上extern “C”. 2.使用共享库对话接口函数生成的对象指针时在该对象未被释放之前不能关闭共享库句柄,否则会出现segmentation fault错误. 以 ...

  8. The 10 best sweet treats in Singapore

    Every time I walk out of Changi airport's air-conditioning into the humid outdoors, there's a sweet ...

  9. Sharepoint的javascript客户端对象模型获取其他站点的list

    获取当前站点(子站点而不是站点集)下的list var clientContext = new SP.ClientContext.get_current(); var list=clientConte ...

  10. SPFA算法学习笔记

    一.理论准备 为了学习网络流,先水一道spfa. SPFA算法是1994年西南交通大学段凡丁提出,只要最短路径存在,SPFA算法必定能求出最小值,SPFA对Bellman-Ford算法优化的关键之处在 ...