【python练习】截取网页里最新的新闻
2016-05-25 18:26
351 查看
需求:
在下面这个网页,抓取最新的新闻,按天划分。
http://blog.eastmoney.com/13102551638/bloglist_0_1.html
实现方法1:使用递归
import urllib
import re
import time
#读取网页内容
content = urllib.urlopen('http://blog.eastmoney.com/13102551638/bloglist_0_1.html').read()
#print content
#截取一部分
pre = re.compile('<li><a href="(.+?)" target="_blank">(.+?)</a><span class="time">(.+?)</span></li>')
new = re.findall(pre,content)
#print new
class News:
#当前年月日
t=int(time.strftime("%Y%m%d ",time.localtime()))
def __init__(self,ct):
self.ct = ct
def search(self):
News.t-=1
#循环这个列表
for item in self.ct:
#列表里,新闻的时间
date = int(item[2][1:5]+item[2][6:8]+item[2][9:11])
#如果新闻是今天发的
if date >= News.t:
#输出这个新闻的标题
title=item[1]
return title
#否则,继续递归search函数
else:
News.search()
aaa=News(new)
cc=aaa.search()
print(cc)
实现方法2:使用while循环
import urllib
import re
import time
#读取网页内容
content = urllib.urlopen('http://blog.eastmoney.com/13102551638/bloglist_0_1.html').read()
#print content
#截取一部分
pre = re.compile('<li><a href="(.+?)" target="_blank">(.+?)</a><span class="time">(.+?)</span></li>')
new = re.findall(pre,content)
#print new
class Good:
def __init__(self,ct):
self.ct = ct
def search(self):
cc=self.ct
i=0
#第一条新闻时间和下一条新闻时间对比,一次类推。如果一样,输出第一条新闻的标题,继续循环
while cc[i][2][0:11] == cc[i+1][2][0:11]:
print(cc[i][1])
i+=1
#如果不一样,输出刚才对比的第一条新闻的标题
else:
print(cc[i][1])
aaa=Good(new)
cc=aaa.search()
在下面这个网页,抓取最新的新闻,按天划分。
http://blog.eastmoney.com/13102551638/bloglist_0_1.html
实现方法1:使用递归
import urllib
import re
import time
#读取网页内容
content = urllib.urlopen('http://blog.eastmoney.com/13102551638/bloglist_0_1.html').read()
#print content
#截取一部分
pre = re.compile('<li><a href="(.+?)" target="_blank">(.+?)</a><span class="time">(.+?)</span></li>')
new = re.findall(pre,content)
#print new
class News:
#当前年月日
t=int(time.strftime("%Y%m%d ",time.localtime()))
def __init__(self,ct):
self.ct = ct
def search(self):
News.t-=1
#循环这个列表
for item in self.ct:
#列表里,新闻的时间
date = int(item[2][1:5]+item[2][6:8]+item[2][9:11])
#如果新闻是今天发的
if date >= News.t:
#输出这个新闻的标题
title=item[1]
return title
#否则,继续递归search函数
else:
News.search()
aaa=News(new)
cc=aaa.search()
print(cc)
实现方法2:使用while循环
import urllib
import re
import time
#读取网页内容
content = urllib.urlopen('http://blog.eastmoney.com/13102551638/bloglist_0_1.html').read()
#print content
#截取一部分
pre = re.compile('<li><a href="(.+?)" target="_blank">(.+?)</a><span class="time">(.+?)</span></li>')
new = re.findall(pre,content)
#print new
class Good:
def __init__(self,ct):
self.ct = ct
def search(self):
cc=self.ct
i=0
#第一条新闻时间和下一条新闻时间对比,一次类推。如果一样,输出第一条新闻的标题,继续循环
while cc[i][2][0:11] == cc[i+1][2][0:11]:
print(cc[i][1])
i+=1
#如果不一样,输出刚才对比的第一条新闻的标题
else:
print(cc[i][1])
aaa=Good(new)
cc=aaa.search()
相关文章推荐
- python 装饰器和 functools 模块
- Python中pass、continue、break、exit()的区别
- Python菜鸟之路:Python基础(三)
- 【转载】python 模块 - random生成随机数模块
- python3 reqeuests给OSC全站用户刷积分
- 【pandas】[4] 数据清洗(数据合并,重塑,转换,离散化,过滤,采样)
- 根据本地文件,生成百度云文件列表
- 人脸验证算法Joint Bayesian详解及实现(Python版)
- 0603 python 基础02
- 用Python+Django1.9在Eclipse环境下开发web网站
- python scrapy环境搭建总结
- 【python】django-celery 实现django项目定时任务
- 0527 python 基础01
- Python模块探秘之EasyGui
- Python模块探秘之EasyGui
- Python-文件对象常用的API
- Python基础学习笔记5 tuple,set
- python递归小疑惑
- second test
- python爬取当前汇率,jython的应用,实时汇率兑换