import urllib
import time
url = ['']*50
i=0
page = urllib.urlopen('http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html').read()
title = page.find(r'<a title=')
href = page.find(r'href=',title)
html = page.find(r'.html',href)
while title!=-1 and href!=-1 and html!=-1 and i < 50:
url[i]= page[href +6:html+5]
print url[i]
title = page.find(r'<a title=',html)
href = page.find(r'href=',title)
html = page.find(r'.html',href)
i+=1
else:
print('Find end!')
j=0
while j<50:
cotent = urllib.urlopen(url[j]).read()
open(r'hanhan/'+url[j][-26:],'w+').write(cotent)
time.sleep(15)
j=j+1
else:
print('Download all!')学习爬虫后写了篇爬韩寒新浪博客文章的代码
最新推荐文章于 2021-02-10 18:16:58 发布
本文介绍了一个简单的Python脚本,该脚本使用urllib库从指定的新浪博客页面抓取文章标题链接并将其内容下载到本地文件中。通过循环查找和解析网页源代码,此脚本能够定位每篇文章的URL并将它们保存为文本文件。

1万+

被折叠的 条评论
为什么被折叠?



