项目概况:Github提供api用于获取项目的数据,利用api就可以编程出实时检测github项目是否更新的代码。当关注的github项目更新时,自动将对应的网页打开。
在客户机和服务器之间进行请求—响应时,两种最常被用到的方法是GET和POST
- GET - 从指定的资源请求数据
- POST - 向指定的资源提交要被处理的数据
Selenium
使用selenium自动操作浏览器。selenium是一个用于Web应用程序测试的工具。selenium测试直接运行在浏览器中,就像真正的用户在操作一样
selenium无法直接控制浏览器,需要中间件,中间件和chrome浏览器之间有对应关系
1、查看浏览器版本 chrome://version
2、中间件—ChromeDriver 下载对应浏览器版本的驱动 ChromeDriver Mirror
3、使用selenium来控制浏览器,先pip安装selenium
from selenium import webdriver
# exe相对于当前1.py的路径,也可以写全路径
CHROMEDRIVER = 'chromedriver.exe'
#1、创建浏览器对象
driver = webdriver.Chrome(executable_path=CHROMEDRIVER)
#2、发送请求
driver.get('https://www.baidu.com')
Chrome显示被自动化测试软件控制,实现自动打开百度
# 自动检测github项目是否更新
import time
import requests
from selenium import webdriver
name = 'psf/requests'
api = 'https://api.github.com/repos/' + name
weburl = 'https://github.com' + name
old_time = None
# 一直监听
while True:
# get方法请求链接
r = requests.get(api)
# 返回200请求成功,不是200请求失败
if r.status_code != 200:
print('请求api失败')
break
'''
怎么判断项目是否更新
time
1、old_time
2、now_time
'''
# json --> dict
now_time = r.json()['updated_at']
if not old_time:
old_time = now_time
if old_time < now_time:
print('项目更新了')
# 项目更新自动打开页面
driver = webdriver.Chrome(executable_path='chromedriver')
driver.get(weburl)
# 60 * 10 --> 10分钟
time.sleep(600)
参考课程:CSDN实用主义学Python