python静态网页爬虫之xpath(简单的博客更新提醒功能)
直接上代码:
#!/usr/bin/env python3
#antuor:Alan
#-*- coding: utf-8 -*- import requests
from lxml import etree
import datetime,time
import os class xxoohelper(object): #易读
def __init__(self):
self.url = 'http://www.cnblogs.com/alan-babyblog/' #初始化
def getSource(self):
html = requests.get(self.url).content #content比text好用,一个返回的是byte,一个返回的是str
return html
def getContent(self,html): #先大后小
selector = etree.HTML(html)
title = selector.xpath('//div[1]/div[2]/a/text()')[0].strip() #从列表提取文本
content = selector.xpath('//div[1]/div[2]/div[1]/div/div[1]/div[3]/div/text()')[0].strip()
post_time = selector.xpath('//div[1]/div[2]/div[1]/div/div[1]/div[5]/text()')[0].strip()
send_text = title+content+post_time #类型是str
return send_text
def tosave(self,text):
with open('myblog.txt','a') as f:
f.write(('{0}\n').format(text)) #换行
def tocheck(self,data):
if not os.path.exists('myblog.txt'): #判断是否存在文件
return True
else:
with open ('myblog.txt','r') as f:
existblog = f.readlines()
#print(data+'\n')
if data +'\n' in existblog: #判断是否已经纪录过内容
return False
else:
return True
if __name__ == '__main__': #程序入口
helper = xxoohelper() #实例化
while True : #while循环不断监控页面
source = helper.getSource()
content = helper.getContent(source)
if helper.tocheck(content):
post_time = str(datetime.datetime.now())
print(post_time,'有新内容\n',content)
helper.tosave(content)
else:
print('扫描中......')
pass
time.sleep(30)
python静态网页爬虫之xpath(简单的博客更新提醒功能)的更多相关文章
- python静态网页爬虫之xpath
常用语句: 1.starts-with(@属性名称,属性字符相同部分)使用情形: 以相同的字符开头 <div id = 'test-1'>需要的内容1</div> <di ...
- Python Web开发:Django+BootStrap实现简单的博客项目
创建blog的项目结构 关于如何创建一个Django项目,请查看[Python Web开发:使用Django框架创建HolleWorld项目] 创建blog的数据模型 创建一个文章类 所有开发都是数据 ...
- Python静态网页爬虫相关知识
想要开发一个简单的Python爬虫案例,并在Python3以上的环境下运行,那么需要掌握哪些知识才能完成一个简单的Python爬虫呢? 爬虫的架构实现 爬虫包括调度器,管理器,解析器,下载器和输出器. ...
- Python编写网页爬虫爬取oj上的代码信息
OJ升级,代码可能会丢失. 所以要事先备份. 一開始傻傻的复制粘贴, 后来实在不能忍, 得益于大潇的启示和聪神的原始代码, 网页爬虫走起! 已经有段时间没看Python, 这次网页爬虫的原始代码是 p ...
- Python动态网页爬虫-----动态网页真实地址破解原理
参考链接:Python动态网页爬虫-----动态网页真实地址破解原理
- [Python爬虫笔记][随意找个博客入门(一)]
[Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...
- 2018-8-10-如何使用-C#-爬虫获得专栏博客更新排行
title author date CreateTime categories 如何使用 C# 爬虫获得专栏博客更新排行 lindexi 2018-08-10 19:16:51 +0800 2018- ...
- 如何使用 C# 爬虫获得专栏博客更新排行
昨天,梦姐问我们,她存在一个任务,找到 关注数排行100 和 浏览量排行100 的专栏博客,在2017年还有更新的专栏. 梦姐说他要出去一趟,M大神在吃饭,于是我估算时间,只有半个钟. 整理一下:半个 ...
- django初探-创建简单的博客系统
django第一步 1. django安装 pip install django print(django.get_version()) 查看django版本 2. 创建项目 打开cmd,进入指定目录 ...
随机推荐
- centos7 ping127.0.0.1不通
ping 127.0.0.1,localhost和本地ip都不通,所有的配置也是正确的 检查下是否禁止了ping vim /proc/sys/net/ipv4/icmp_echo_ignore_all ...
- docker 安装 gogs(go git server) 及问题解决
docker安装gogs 参考官方说明 gogs mysql 支持 运行一个mysql image docker run --name gogs-mysql -e MYSQL_ROOT_PASSWOR ...
- [Spring boot] Application properties and configurations
We can use different application properties application.properties: server.port=9090 application-pro ...
- ios app在itunesConnect里面的几种状态
原地址:http://blog.csdn.net/dean19900504/article/details/8164734 Waiting for Upload (Yellow) Appears wh ...
- JSON--百度百科
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...
- 帮朋友转发招聘信息 南京知名互联网公司招聘java、测试、前端,具体私聊
一.java开发 1.5年及以上J2EE方向开发经验 2.精通spring等开源框架 3.熟悉html.javascript.css.jsp/freemarker.jquery的使用 4.熟悉使用my ...
- 用JDOM读取XML文件
用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类.Element类等的方法读取所需的内容.IB ...
- 【Nginx】核心模块ngx_events_module
核心模块ngx_events_module是一个专门用于管理事件模块的模块. 它的实现非常easy.以下是该模块的定义: ngx_module_t ngx_events_module = { NGX_ ...
- 网络方面的常用命令 & 常用端口介绍
在网络方面我们常常会用到如下命令: (1)ping命令:我们常常用来判断2台或2台以上的机器间是否网络连通. ping 192.168.1.88 -t 如果想看任何命令的参数是什么意思,我们只需要:命 ...
- ios 中尝试多次请求
-(void)tryRun { tryTimes++; id obj = [ASODataManager getAppleAccount]; if (obj) { __block FirstViewC ...