网站内容更新监控(Python领域的大神级人物,在Github中打开这些库的地址 )
优采云 发布时间: 2021-09-12 18:06网站内容更新监控(Python领域的大神级人物,在Github中打开这些库的地址
)
[目录]
监控 Github 项目更新并自动打开网页
# -*- coding:utf-8 -*-
"""
@author: XueHaozhe
@file:检测github并自动打开网页.py
@time:2018/11/30 16:21
"""
# api https://api.github.com/repos/channelcat/sanic
# web_page https://github.com/channelcat/sanic
import requests
import webbrowser
import time
api = 'https://api.github.com/repos/channelcat/sanic'
web_page = 'https://github.com/channelcat/sanic'
last_update = '"2018-10-30T08:11:59Z"'
dict_info = requests.get(api).json()
cur_update = dict_info['updated_at']
while True:
if not last_update:
last_update = cur_update
if last_update < cur_update:
webbrowser.open(web_page)
time.sleep(600)
Kenneth Reitz 是 Python 领域的杰出人物,在 Github 上非常活跃。他的Github地址是:试着用你所学的去发现Kenneth Starred今天有哪些库,并在浏览器中自动打开这些库的地址。
问题解决提示:
· 这个问题可以分解为以下子问题:
·1.如何获取指定用户的明星项目?
·2.如何判断新品的出现?
·3.如何打开网页?
·4.如何定期监控程序?
问题解决技巧:
·1.使用GitHub提供的api获取指定用户的star的所有项目,并转换为json数据。然后,提取所有 id 字段并将它们存储在列表变量中。此变量是用户已加星标的项目列表。 api调用格式为:其中kennethreitz为用户名,为可变参数。
·2. 重复1中的步骤,然后将列表变量与刚刚抓取的数据进行比较。如果 item id 不在列表变量中,则表示该 item 是新的。
·3.使用webbrowser模块,其中open函数可以控制浏览器打开指定页面。
·4.while True 语句是一个无限循环,即可以无限循环的语句,并且这个语句可以控制程序一直运行。 time模块中的sleep函数可以让程序休眠一段时间,从而达到定时重新运行的效果。
# -*- coding:utf-8 -*-
"""
@author: XueHaozhe
@file:Kenneth 今天 Star 了哪些库.py
@time:2018/11/30 17:22
"""
import requests
import webbrowser
import time
api = "https://api.github.com/users/kennethreitz/starred"
data = requests.get(api).json() #[{}]
starred = []
for i in data:
starred.append(i['id'])
while True:
info = requests.get(api).json()
for i in info:
# 如果当前项目id在list变量中不存在,则说明是刚刚star的项目
if not i['id'] in starred:
starred.append(i['id'])
repo_name = i['name']
owner = i['owner']['login']
# 在浏览器中打开项目
web_page = "https://github.com/" + owner + "/" + repo_name
webbrowser.open(web_page)
time.sleep(600) #十分钟