捕获所有文章的超级链接(中篇)-python爬虫

目标:下载韩寒博客首页的所有文章(共50篇文章)

python代码

#<a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102wrup.html">写给那个茶水妹的《乘风破浪》诞生…</a>
#coding:utf-8
import urllib.request as urllib
import time
url = ['']*50#url列表,存储50篇文章
con = str(urllib.urlopen('http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html').read(),'utf-8')
i = 0
title = con.find(r'<a title=')
href = con.find(r'href=',title)#从title后开始搜索href
html = con.find(r'.html',href)#从href后开始搜索.html
#捕获文章所有地址
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(r'<a title=',html)
 href = con.find(r'href=',title)#从title后开始搜索href
 html = con.find(r'.html',href)#从href后开始搜索.html
 i = i + 1
 
else:
    print ('find end!')
#下载所有文章    
j = 0
while j < 50:
    content = str(urllib.urlopen(url[j]).read(),'utf-8')
    open(r'韩寒/'+url[j][-26:],'w+',1,'utf-8').write(content)
    print('downloading',url[j])
    j = j + 1
    time.sleep(5)
else:
    print('download article finished')



如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值