◎软件环境:
◎功能描述:
对于喜欢历史的同学来说,生活中的每一天都在创造历史,而对于人类历史长河来说,个人也只是沧海一粟,历史让人短暂有限的人生延长了。通过查看历史上的今天,回顾过去,吸取前人的教训,争取美好的未来,每天了解一下过去这一天发生了什么,日积月累,不仅知识增长了,也可以了解历史的进程,给生活增加点乐趣。
◎实现思路:
通过requests去调用历史上的今天网站的查询接口,bs4去抓取返回页面元素获取每一天的历史事件,回车直接跳转到内容页面,按shift键可以预览网页内容,输入ls默认取当天的历史纪录,ls 日期(x/x格式)可以查看指定日期的历史事件。第一次加载由于要下载图片比较慢,之后就好了。
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import sys, os
import requests
from bs4 import BeautifulSoup
from workflow import Workflow3
reload(sys)
sys.setdefaultencoding('utf-8')
def save_img(url):
"""下载图片"""
file_name = url.split('/')[-1]
try:
r = requests.get(url)
except:
print "远程连接错误"
try:
filepath = "picture/" + file_name
with open(filepath, 'wb') as f:
f.write(r.content)
except:
print "文件保存错误"
def main(wf):
"""输入日期"""
base_url = "https://www.lssdjt.com"
if not os.path.exists("picture"):
os.makedirs("picture")
if not wf.args:
url = base_url
else:
try:
today = str(wf.args[0])
month = today.split('/')[0]
day = today.split('/')[1]
url = "https://www.lssdjt.com/{}/{}/".format(month, day)
except:
print "输入参数错误"
resp = requests.request("GET", url)
resp.raise_for_status()
soup = BeautifulSoup(resp.content, "html.parser")
today_in_history = soup.find_all("li", attrs={"class": "gong"})
"""加载资源"""
try:
threads = []
for li in today_in_history:
h_url = base_url + li.a.attrs['href']
if li.a.has_attr('rel'):
pic_url = li.a.attrs['rel'][0]
pic_name = pic_url.split('/')[-1]
h_jpg = "picture/" + pic_name
if not os.path.exists(h_jpg):
t = threading.Thread(target=save_img,args={pic_url})
threads.append(t)
else:
h_jpg = 'default.png'
h_date = li.a.em.get_text("", strip=True)
h_title = li.a.i.get_text("", strip=True)
wf.add_item(title=h_title, subtitle=h_date, arg=h_url, valid=True, icon=h_jpg)
for i in threads:
i.start()
for i in threads:
i.join()
except:
print "资源加载失败"
wf.send_feedback()
if __name__ == '__main__':
wf = Workflow3()
sys.exit(wf.run(main))◎效果展示:
◎下载地址:https://github.com/anzhihe/Efficient-office/tree/master/today-in-history

大佬,为啥我测试了ls 6/4可以显示,其他日期就调用不出来呢?
@明天会秃头吗 是第一次下图片有点慢,你稍等会就出来了