需求:

在下面这个网页,抓取最新的新闻,按天划分。

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练习】截取网页里最新的新闻的更多相关文章

  1. python 获取一个网页里的a 标签

    #!/usr/bin/python # -*- coding: utf-8 -*- #encoding=utf-8 #Filename:urllib2-header.py import re impo ...

  2. Python 网络爬虫 008 (编程) 通过ID索引号遍历目标网页里链接的所有网页

    通过 ID索引号 遍历目标网页里链接的所有网页 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyChar ...

  3. python爬虫抓网页的总结

    python爬虫抓网页的总结 更多 python 爬虫   学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自 ...

  4. 如何使用python爬取网页动态数据

    我们在使用python爬取网页数据的时候,会遇到页面的数据是通过js脚本动态加载的情况,这时候我们就得模拟接口请求信息,根据接口返回结果来获取我们想要的数据. 以某电影网站为例:我们要获取到电影名称以 ...

  5. python字符串截取与替换的例子

    python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...

  6. Python HTMLTestRunner生成网页自动化测试报告时中文编码报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6

    1. 由于使用Python Selenium做网页自动化测试时,有截取网页上的中文信息保存到测试结果中,最终出现编码错误如下: File "D:/PycharmProjects/AutoTe ...

  7. python - selenium 2 升级到最新版本

    python - selenium 2 升级到最新版本 之前一直用的是selenium 2.48 .firefox36 而实际用户的浏览器可能都有自动更新功能,所以版本基本上是最新的.所以这次专门做了 ...

  8. python 简单爬取今日头条热点新闻(一)

    今日头条如今在自媒体领域算是比较强大的存在,今天就带大家利用python爬去今日头条的热点新闻,理论上是可以做到无限爬取的: 在浏览器中打开今日头条的链接,选中左侧的热点,在浏览器开发者模式netwo ...

  9. Python爬取网页信息

    Python爬取网页信息的步骤 以爬取英文名字网站(https://nameberry.com/)中每个名字的评论内容,包括英文名,用户名,评论的时间和评论的内容为例. 1.确认网址 在浏览器中输入初 ...

随机推荐

  1. commonJS — 函数操作(for Function)

    for Function github: https://github.com/laixiangran/commonJS/blob/master/src/forFunction.js 代码 /** * ...

  2. Java软件工程师全栈技能

    1.前端基础技能,掌握html.js.css,会用jquery.bootstrap. 2.前端技能升级,会用echart.BMap等等. 3.前端高级技能,熟练使用angularjs等等. 4.服务端 ...

  3. go语言实战向导

    版权声明:本文由魏佳原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/173 来源:腾云阁 https://www.qclou ...

  4. html年月日下拉联动菜单 年月日三下拉框联动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Unity5 新功能解析--物理渲染与standard shader

    Unity5 新功能解析--物理渲染与standard shader http://blog.csdn.net/leonwei/article/details/48395061 物理渲染是UNITY5 ...

  6. computer English

    算法常用术语中英对照Data Structures 基本数据结构Dictionaries 字典PriorityQueues 堆Graph Data Structures 图Set Data Struc ...

  7. jq--回到顶部

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. ASP.NET中EVAL用法大全

    <%# Bind("Subject") %> //绑定字段<%# Container.DataItemIndex + 1%> //实现自动编号<%# ...

  9. css样式初始化

    不同的浏览器对有些标签的默认显示是不同的,对css样式初始化可以实现样式的统一,消除不同浏览器间页面显示的差异性... 一般初始化方式为:*{margin:0:padding:0:}

  10. hduoj------2594 Simpsons’ Hidden Talents

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...