Python 爬虫学习 urllib
- 网页抓取
# -*-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") - 获取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] - 获取网页状态码
# -*-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 - 保存网页内容
# -*-coding: utf-8 -*-
# Author:Evilxr import urllib url = "http://www.cdnzz.com/" urllib.urlretrieve(url, "d:\\evilxr.html") - 获取网站编码类型
# 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] - 自动获取网站编码 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的更多相关文章
- python爬虫学习(1) —— 从urllib说起
0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...
- python爬虫学习 —— 总目录
开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...
- Python爬虫学习:三、爬虫的基本操作流程
本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...
- 《Python爬虫学习系列教程》学习笔记
http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...
- Python爬虫之urllib模块2
Python爬虫之urllib模块2 本文来自网友投稿 作者:PG-55,一个待毕业待就业的二流大学生. 看了一下上一节的反馈,有些同学认为这个没什么意义,也有的同学觉得太简单,关于Beautiful ...
- python爬虫学习笔记(一)——环境配置(windows系统)
在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库) [推荐地址:清华镜像] https://mirrors ...
- [转]《Python爬虫学习系列教程》
<Python爬虫学习系列教程>学习笔记 http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多. ...
- Python爬虫学习第一记 (翻译小助手)
1 # Python爬虫学习第一记 8.24 (代码有点小,请放大看吧) 2 3 #实现有道翻译,模块一: $fanyi.py 4 5 import urllib.request 6 import u ...
- Python爬虫学习:四、headers和data的获取
之前在学习爬虫时,偶尔会遇到一些问题是有些网站需要登录后才能爬取内容,有的网站会识别是否是由浏览器发出的请求. 一.headers的获取 就以博客园的首页为例:http://www.cnblogs.c ...
随机推荐
- CAD迷你看图
CAD迷你看图http://www.aec188.com/CAD迷你看图 2016R12超快.超小的CAD多功能看图工具,完全脱离AutoCAD浏览R14-R2016各版本DWG/DXF/DWF的二三 ...
- webwork
代码结构,html页面引入两个js文件,work.js和main.js work.js (function () { onmessage = function (e) { var num = e.da ...
- ImportError: No module named MySQLdb
ImportError: No module named MySQLdb 该错误是源于我们没有安装Python连接MySQL所需的MySQLdb库而引起. python3.5下的解决方法ubuntu系 ...
- Ubuntu配置pyethapp
1. 安装系统依赖 apt-get install build-essential automake pkg-config libtool libffi-dev libgmp-dev 2. Clone ...
- Linux下I/O模型
Unix下共有五种I/O模型 1. 阻塞式I/O 2. 非阻塞式I/O 3. I/O复用(select和poll) 4. 信号驱动式I/O(SIGIO) 5. 异步I/O(POSIX的aio_系列函数 ...
- 【html5】常见标签使用说明(持续更新)
说明: 所谓常见,是指我在优秀网页源码中见到的. 1.viewport 我见到的时候是这样: <meta name="viewport" content="widt ...
- iOS中的translucent和automaticallyAdjustsScrollViewInsets用法
关于这两个属性我长话短说 具体的可以更具具体情况来设置: translucent用法 automaticallyAdjustsScrollViewInsets用法 translucent用法 iOS7 ...
- Rest接口测试,巧用firebug插件
两年前开始做软件测试,刚接触的是关于rest接口的测试.作为一个刚进职场的测试小菜鸟,当时对接口的理解并不是很充分,具体是怎么实现的也不清楚.在进行接口测试时,只是设置接口入参,调用接口,查看接口是否 ...
- hdu 4747 Mex
http://acm.hdu.edu.cn/showproblem.php?pid=4747 设我们输入的数组为 a[],我们需要从 1 到 n 遍历, 假设遍历到 i 时, 遍历的过程中用b[j]表 ...
- android技巧(三)屏幕适配
屏幕适配策略: 1.控件使用wrap_content.match_parent控制某些视图组件的宽度和高度,而不是硬编码的尺寸. “wrap_content”系统就会将视图的宽度或高度设置成所需的最小 ...