拥有一台24小时运行的服务器以后,不折腾一下怎么行呢?
如果还不会搭建服务器,戳这里 【如何打造一台外网可以访问的服务器】
正好前几天小区停水10小时,物业没有通知,
部署爬虫
python安装
apt-get install python3
安装模块包
参考文章链接
apt-get install python-<packageName>
比如说安装本文中将要用到的beautifulSoup模块:
apt-get install python-bs4
编写爬虫
爬虫文件和数据库位于同一台机器上,直接让爬虫将数据写入数据库是更加效率的做法,然而python操作mysql需要使用的mysqldb无法快速安装(修正:可以通过sudo apt-get install python3-mysqladb
安装),同时我们有现成的servlet可以使用,故选择使用爬虫调用servlet接口,由servlet接口写入数据库。想要了解如何安装mysqldb模块可以戳这里【Linux下使用Python操作MySQL数据库】
#coding=utf-8
import requests
from bs4 import BeautifulSoup
def getHtml(url):
response = (requests.get(url = url))
return response.content.decode("gbk")#该网站编码格式为gbk
def get_cqzls():
base_url = "http://www.cqzls.com/html/tstz/"
html = getHtml(base_url)
soup = BeautifulSoup(html, 'html.parser')
for soup2 in soup.find_all("li") :
time = soup2.find("span").text.strip()
title = soup2.find("a").get("title")
href = "http://www.cqzls.com"+soup2.find("a").get("href")
if '南岸区' in title:
doPost(time,title,href)
def doPost(time,title,href):
url = 'http://localhost/myRaspbian2/get_notice'
data = {"url":href,"title":title,"notice_time":time}
r = requests.post(url,data)
print(r.text)
get_cqzls()
部署servlet
如果还不会部署servlet可以戳我的另一篇文章如何创建并部署servlet
import com.alibaba.fastjson.JSON;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/get_notice")
public class get_notice extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
try {
request.setCharacterEncoding("UTF-8");//参数含有中文
String title = request.getParameter("title");
String url = request.getParameter("url");
String notice_time = request.getParameter("notice_time");
if (title == null || url == null || notice_time == null) {
out.println("缺少参数");
}
Map<String, Object> resultMap;
String res;
List list = DateBaseHelper.getData("select * from water_supplay_notice where title='" + title + "' and notice_time='" + notice_time + "';");
if (list!=null &&list.size() == 0) {
resultMap = DateBaseHelper.insertData("insert into water_supplay_notice(title,url,notice_time) values('" + title + "','" + url + "','" + notice_time + "');");
resultMap.put("message","记录成功");
res = JSON.toJSONString(resultMap);
out.println(res);
}else{
resultMap = new HashMap<>();
resultMap.put("resultMap","-1");
resultMap.put("message","已有该条公告");
res = JSON.toJSONString(resultMap);
out.println(res);
}
} catch (Exception e) {
out.print("异常:" + e.getMessage());
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.print("收到get");
}
}
DateBaseHelper的代码在如何部署servlet那边文章里已经贴出来了,这里就不重复贴了。
爬虫测试
python上传到服务器,servlet也部署完成以后,运行一下试试吧
测试OK
部署定时任务cron
cron服务管理
/etc/init.d/cron start
启动
/etc/init.d/cron stop
停止
/etc/init.d/cron restart
重启
添加定时任务
crontab -e
命令打开当前用户的定时任务配置文件
添加定时任务
* 0,12 * * * python /home/pi/get_notice_from_cqzls.py
意思是每天12点,24点运行一次py文件
这行代码并不能按照预期来运行,实际效果是0点和12点每分钟执行一次任务,正确的写法应该是
0 0,12 * * * python /home/pi/get_notice_from_cqzls.py
0点0分和12点0分执行一次任务,前两天看着日志文件里每天打印的两个小时消息我陷入了沉思。。。
保存,然后重启cron
定时任务测试
清空数据表和日志文件,明天再来看看。
第二天,打开数据表看一下数据的写入时间。
25号0点添加的数据,和定时任务设置的一样,测试完成