爬虫笔记

在python中打开网页

import webbrowser as web
web.open_new_tab('http://www.baidu.com')

获取网页状态码

#coding:utf-8
import urllib
url = 'http://www.baidu.com'
html = urllib.urlopen(url)
print html.getcode()

输出:

200

HTTP状态码表示HTTP协议所返回的响应的状态。下面将状态码归结如下:

  • 100:继续 客户端应当继续发送请求。客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应
  • 101: 转换协议 在发送完这个响应最后的空行后,服务器将会切换到在Upgrade 消息头中定义的那些协议。只有在切换新的协议更有好处的时候才应该采取类似措施
  • 102:继续处理 由WebDAV(RFC 2518)扩展的状态码,代表处理将被继续执行
  • 200:请求成功 处理方式:获得响应的内容,进行处理
  • 201:请求完成,结果是创建了新资源。新创建资源的URI可在响应的实体中得到 处理方式:爬虫中不会遇到
  • 202:请求被接受,但处理尚未完成 处理方式:阻塞等待
  • 204:服务器端已经实现了请求,但是没有返回新的信 息。如果客户是用户代理,则无须为此更新自身的文档视图。 处理方式:丢弃
  • 300:该状态码不被HTTP/1.0的应用程序直接使用, 只是作为3XX类型回应的默认解释。存在多个可用的被请求资源。处理方式:若程序中能够处理,则进行进一步处理,如果程序中不能处理,则丢弃
  • 301:请求到的资源都会分配一个永久的URL,这样就可以在将来通过该URL来访问此资源。处理方式:重定向到分配的URL
  • 302:请求到的资源在一个不同的URL处临时保存。处理方式:重定向到临时的URL
  • 304:请求的资源未更新 处理方式:丢弃
  • 400:非法请求 处理方式:丢弃
  • 401:未授权 处理方式:丢弃
  • 403:禁止 处理方式:丢弃
  • 404:没有找到 处理方式:丢弃
  • 500:服务器内部错误 服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理。一般来说,这个问题都会在服务器端的源代码出现错误时出现
  • 501:服务器无法识别 服务器不支持当前请求所需要的某个功能。当服务器无法识别请求的方法,并且无法支持其对任何资源的请求
  • 502:错误网关 作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应

获取网页头部信息

#coding:utf-8
import urllib
url = 'http://www.baidu.com'
html = urllib.urlopen(url)
print html.info()

输出:

Date: Mon, 11 Jul 2016 00:51:52 GMT
Content-Type: text/html; charset=utf-8
Connection: Close
Vary: Accept-Encoding
Set-Cookie: BAIDUID=3F0D3078E1D6BB9B9ADC34311920C87B:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=3F0D3078E1D6BB9B9ADC34311920C87B; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1468198312; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=1463_20516_18241_20536_20416_20456_19860_17001_15297_11941; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Cache-Control: private
Cxy_all: baidu+0afd943e0bb8e6444c0571f9b871d94a
Expires: Mon, 11 Jul 2016 00:51:47 GMT
X-Powered-By: HPHP
Server: BWS/1.1
X-UA-Compatible: IE=Edge,chrome=1
BDPAGETYPE: 1
BDQID: 0xb7482c7500027c9c
BDUSERID: 0

爬取一个网页之前一定要查看网页的状态码和头部信息

根据网页编码方式的不同爬取网页

  • 网页编码为utf-8:可直接读取,不会出现乱码问题
#coding:utf-8
import urllib

url = 'http://www.baidu.com'
html = urllib.urlopen(url)
print html.read()

输出:

<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" /><link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu.svg"><link rel="dns-prefetch" href="//s1.bdstatic.com"/><link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/><link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/><link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/><link rel="dns-prefetch" href="//b1.bdstatic.com"/><title>百度一下,你就知道</title>
  • 网页编码不为utf-8编码:若使用上述方法将出现乱码问题
<!DOCTYPE html>
<html id="spLianghui">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>����</title>
<link rel="dns-prefetch" href="http://img1.cache.netease.com" />
<base target="_blank" />
<meta name="Keywords" content="����,����,��Ϸ,����,����,����,Ů��,����,��̳,����,����,����,�ֻ�,�ƾ�,�Ƽ�,���" />
<meta name="Description" content="�������й����ȵĻ�����������˾��Ϊ�û��ṩ������䡢��Ϸ��������������������š����֡�������30�������Ƶ���������͡���Ƶ����̳�Ȼ��������������˵�������" />
<meta name="robots" content="index, follow" />
<meta name="googlebot" content="index, follow" />

解决办法:将其转化为utf-8
使用命令:print html.read().decode('gbk').encode('utf-8')

#coding:utf-8
import urllib

url = 'http://www.163.com'
html = urllib.urlopen(url)
print html.read().decode('gbk').encode('utf-8')

输出:

 <!DOCTYPE html>
<html id="spLianghui">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>网易</title>
<link rel="dns-prefetch" href="http://img1.cache.netease.com" />
<base target="_blank" />
<meta name="Keywords" content="网易,邮箱,游戏,新闻,体育,娱乐,女性,亚运,论坛,短信,数码,汽车,手机,财经,科技,相册" />
<meta name="Description" content="网易是中国领先的互联网技术公司,为用户提供免费邮箱、游戏、搜索引擎服务,开设新闻、娱乐、体育等30多个内容频道,及博客、视频、论坛等互动交流,网聚人的力量。" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值