python提取百度首页_python爬虫百度首页爬取

前言

经受不住爬虫技术的吸引,为此决定踏入”爬虫”这条不归路。

爬虫介绍

其实在我眼里,爬虫无非所见即所得,也就是一切皆可爬。至于url技术和python环境在此就不重复。在此使用urllib库进行初步学习。

python:2.7

初次尝试

网上代码实现:

import urllib2

response = urllib2.urlopen("http://www.baidu.com")

print response.read()

结果返回:

? 峍Ko??*嵔炙?G?l户??

擧蒐$QKQ~琣?贩瀦j/砉?姸?婨O?J?荌惸崆y|3r鮣揥??s?恪~SL泼擩 b櫅鬽?#騆襆歳漇d I簰禯鑷s,

*G?zD脥ss掠K$m%/В)?,JXv殞P1鏐啣4缾14!;?5多? ?窃^?n她镞咻嘷阆簖挟!肂?湰7s殊刽\莥禪?_P%|i?\J经P8砉絴e`羛?8紜/3鈛?狜菁q憊摔洶M奅?像薭鳮X!褪緱駥謰話M?&? xZ?K鉓?÷s秞騽 8?{8T怓?[栧ボ狩&窛ダyN疟 笖帮?-啣蕹茘儤*VzU&!粏阕~灍箛?4揀.4[>?珮2墼J.XO聊.蓅芲]愄偨{/Y?漼H謶猇7??#P*`o憽床\*場R+埢;胘?J鴏Z裊m{ez叻?湯RW諩牉fr[阢DSO?h?溒~5札纘郥0PO??啞?{蛅?+R?鞖u? 跴?

k簵愖硇]躁?员9Bp(辣?H芈`d劥茤D-颐0罞QKf嘆=S货榣v?塧 g?椤K KQRT?尷=莱礤ri槕?

yj???w?婹曀闿8∟?菇K衚儠馪Y甽Une8呌?B???

寯险Au盆將NJ鴎n*OB脃?斕T@矄?6氻偬??焥蒛tJ?P搘K瓃I袔wX斌&奴刦qEV繌TBsh?耍h葧骇Dw闘>胶箊su3橪^Oψ_L焈繗線锼阻礔晦H页na鉺淯w?箮5班a?UDZSu? 猂? Traceback (most recent call last):

File "", line 1, in

IOError: [Errno 0] Error

心情表示很无奈,刚开始我认为是编码问题,要不然怎么会乱码,结果上网找了若干方法并未实现效果。

代码改进

最后这篇文章给了我灵感。

我猜想可能是压缩格式的缘故。

代码实现:

# -*- coding: utf-8 -*-

import urllib2

import gzip

import StringIO

url = 'http://www.baidu.com'

data = urllib2.urlopen(url).read()

data = StringIO.StringIO(data)

gzipper = gzip.GzipFile(fileobj=data)

fp = open('1.txt','w')

fp.write(gzipper.read())

返回结果:

1.txt

百度一下,你就知道

满满的百度首页代码,表示很欣喜。

通过submit text3里面的HTML/CSS/JS Prettify格式化返回代码,我们可以很清晰的看到代码。

百度一下,你就知道

html,

body {

height: 100%

}

html {

overflow-y: auto

}

body {

font: 12px arial;

background: #fff

}

body,

p,

form,

ul,

li {

margin: 0;

padding: 0;

list-style: none

}

body,

form {

position: relative

}

img {

border: 0

}

a {

color: #00c

}

a:active {

color: #f60

}

input {

border: 0;

padding: 0

}

#wrapper {

position: relative;

_position: ;

min-height: 100%

}

#head {

padding-bottom: 100px;

text-align: center;

*z-index: 1

}

#wrapper {

min-width: 810px;

height: 100%;

min-height: 600px

}

#head {

position: relative;

padding-bottom: 0;

height: 100%;

min-height: 600px

}

#head .head_wrapper {

height: 100%

}

#form {

margin: 22px auto 0;

width: 641px;

text-align: left;

z-index: 100

}

#kw {

position: relative

}

.s_btn {

width: 95px;

height: 32px;

padding-top: 2px\9;

font-size: 14px;

background-color: #ddd;

background-position: 0 -48px;

cursor: pointer

}

.s_btn {

width: 100px;

height: 36px;

color: white;

font-size: 15px;

letter-spacing: 1px;

background: #3385ff;

border-bottom: 1px solid #2d78f4;

outline: medium;

*border-bottom: 0;

-webkit-appearance: none;

-webkit-border-radius: 0

}

.s_btn_wr {

width: 97px;

height: 34px;

display: inline-block;

background-position: -120px -48px;

*position: relative;

z-index: 0;

vertical-align: top

}

.s_btn_wr {

width: auto;

height: auto;

border-bottom: 1px solid transparent;

*border-bottom: 0

}

.s_ipt_wr {

height: 34px

}

.s_ipt_wr.bg,

.s_btn_wr.bg,

#su.bg {

background-image: none

}

.s_ipt_wr {

border: 1px solid #b6b6b6;

border-color: #7b7b7b #b6b6b6 #b6b6b6 #7b7b7b;

background: #fff;

display: inline-block;

vertical-align: top;

width: 539px;

margin-right: 0;

border-right-width: 0;

border-color: #b8b8b8 transparent #ccc #b8b8b8;

overflow: hidden

}

.s_ipt {

width: 526px;

height: 22px;

font: 16px/18px arial;

line-height: 22px\9;

margin: 6px 0 0 7px;

padding: 0;

background: transparent;

border: 0;

outline: 0;

-webkit-appearance: none

}

.s_form {

position: relative;

top: 38.2%

}

.s_form_wrapper {

position: relative;

top: -191px

}

var md5 = "230CFBXBZBXCCCDBYCEDREADTEHDREIDZ"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值