#python爬虫实例 爬取新闻#爬取新浪新闻首页中所有的新闻,爬到本地(http://news.sina.com.cn/)#实现过程,先爬首页,通过正则表达式获取所有新闻链接,然后依次爬各新闻,并存储到本地import urllib.requestimport redata=urllib.request.urlopen("http://news.sina.com.cn/").read()data2=data.decode("utf-8","ignore")pat='href="(http://news.sina.com.cn/.*?)"'allurl=re.compile(pat).findall(data2)for i in range(0,10): try: print("第"+str(i)+"次爬取") thisurl=allurl[i] file="/Users/xubin/myapp/pythonfile/sina/"+str(i)+".html" urllib.request.urlretrieve(thisurl,file) print("------成功-------") except urllib.error.URLError as e: if hasattr(e,"code"): print(e.code) if hasattr(e,"reason"): print(e.reason) 继续阅读与本文标签相同的文章
上一篇 :
4.python爬虫浏览器伪装技术
下一篇 :
python常用算法
-
[雪峰磁针石博客]使用python3和flask构建RESTful API(接口测试服务)
2026-05-25栏目: 教程
-
连续四天!2018杭州·云栖大会你可以...
2026-05-25栏目: 教程
-
Redis 和 Memcached 的区别
2026-05-25栏目: 教程
-
Java程序员涨薪必备技能(1-3年必看!)
2026-05-25栏目: 教程
-
12年来最大飞跃!黄仁勋发布史上最强GPU,世界首个实时光线追踪新一代图灵架构
2026-05-25栏目: 教程
