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 ...
随机推荐
- python 线程学习
彩照 一.学习[1] # -*- coding: utf-8 -*- import time import thread def timer(no, interval): cnt = 0 while ...
- 学习mongo系列(九)索引,聚合,复制(副本集),分片
一.索引 二.聚合 三.复制(副本集) 四.分片 尚未实践操作. 详见http://www.runoob.com/mongodb/mongodb-indexing.html
- js模板引擎
js模板引擎包括如下: template 官方参考:http://aui.github.io/artTemplate BaiduTemplate 官方参考:http://baidufe.github. ...
- java高薪之路__009_网络
1. InetAddress类2. Socket: IP地址和端口号的结合,socket允许程序把网络连接当成一个流,数据在两个socket间通过IO传输, 通信的两端都要有socket. 主动发起通 ...
- JS_call_APP native 与 html的交互
1.***** 特点:下个版本的交互准备使用这个(http://www.knowsky.com/884428.html) https://github.com/lifei321/JS-OC http: ...
- Android之alertDialog、ProgressDialog
一.alertDialog 置顶于所有控件之上的,可以屏蔽其他控件的交互能力.通过AlertDialog.Builder创建一个AlertDialog,并通过setTittle(),setMesseg ...
- 0505 Scrum 项目1.0
应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 团队项目选题 一个售书网站(O2O) NABCD 模型 1) N (N ...
- JavaScipt 源码解析 异步
我们常见的异步操作: 定时器setTimeout postmessage WebWorkor CSS3 动画 XMLHttpRequest HTML5的本地数据 等等- JavaScript要求在与服 ...
- python中列表的常用方法
s=[1,2,3] s[3]=12#列表长度小于3时无法给列表赋值 len(s)#列表长 s+s s*5#l列表重复5次 5 in s#判断元素是否在列表中,返回true or false max(s ...
- Struts2 报 Result 错误
写的时候犯了个低级错误 struts.xml中 配置result 的时候 没有配置type