网页抓取
### -*- coding: cp936 -*-
###<a href="http://home.51cto.com" target="_blank">家园</a>
##import urllib
##str0=‘<a href="http://home.51cto.com" target="_blank">家园</a>‘
##href=str0.find(‘<a href‘)
##print href
##com=str0.find(‘.com"‘)
##print com
##url=str0[href+9:com+4]
##print url
##content=urllib.urlopen(url).read()
###print content
##filename=url[-9:]
##print filename
##open(filename,‘w‘).write(content)
####_________________________________
import urllib
url = [‘‘]*50
con = urllib.urlopen(‘http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html‘).read()
i = 0
title = con.find(r‘<a title=‘)
href = con.find(‘href=‘,title)
html = con.find(‘.html‘,href)
while title !=-1 and href != -1 and html != -1 and i < 50 :
url[i] = con[href + 6:html + 5]
print url[i]
title = con.find(‘<a title=‘,html)
href = con.find(‘href=‘,title)
html = con.find(‘.html‘,href)
i = i + 1
else:
print ‘find end!‘
j = 0
while j < 50:
content = urllib.urlopen(url[j]).read()
open(‘hanhan/‘+url[j][-26:],‘w‘).write(content)
j = j + 1
else:
print "over"
##
##--------------------------------------------
本文出自 “sai” 博客,请务必保留此出处http://qingsto.blog.51cto.com/3570923/1535126
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。