新浪博客python_用python2.7,采集新浪博客

#coding=utf-8 #新浪博客

import urllib

import re

import os

url=['']*1500 #每一骗博客的地址

title=['']*1500 #每一篇博客的标题

page=1 #博客分页

count=1 #文章计数

while page<=9:

con=urllib.urlopen('http://blog.sina.com.cn/s/articlelist_1193491727_0_'+str(page)+'.html').read()

i=0

hrefstart=con.find(r'href="http://blog.sina.com.cn/s/blog_')

print hrefstart

hrefend=con.find(r'.html',hrefstart)

print hrefend

titlestart=con.find(r'>',hrefend)

print titlestart

titleend=con.find(r'',titlestart)

print titleend

while i<=50 and titleend!=-1 and hrefend!=-1:

url[i]=con[hrefstart+6:hrefend+5]

title[i]=con[titlestart:titleend]

print page,i,count, title[i]

print url[i]

hrefstart=con.find(r'href="http://blog.sina.com.cn/s/blog_',titleend)

hrefend=con.find(r'.html',hrefstart)

titlestart=con.find(r'>',hrefend)

titleend=con.find(r'',titlestart)

content=urllib.urlopen(url[i]).read()

filename=url[i][-26:]

print filename

if not os.path.isdir("1"):

os.mkdir("1")

target=open('1/'+filename,'w')

target.write(content)

i=i+1

count=count+1

else:

print page,'本页查找到结尾了'

page=page+1

else:

print'本次任务结束了'

用python2.7,采集新浪博客,王石的博客文章。

实现了文章列表多页采集,实现了下载到本地。

练手之做,如果有更好的代码,也分享一些给我

欢迎交流

还有几点未作:

1、利用正则实现提取每一页的文章内容。

2、目录按照下载时间自动命名

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值