作一个座右铭工具天天激励本身

想法

新年第一篇文章,最近作英语相关的服务作多了,发现有人在作座右铭的东西,我也有了个想法就是想作一个能够随机一个座右铭的工具,天天激励本身,或者看看别人怎么激励本身,顺便学英语。html

实施

技术选型就是lumen+python(爬虫)+js bin+mongopython

爬虫

首先我找了个网站,https://www.brainyquote.com/ ,国外的专门搜集和作UGC座右铭的,感受还不错,内容丰富,质量很高,还能学英语 ,举例:I believe that if one always looked at the skies, one would end up with wings.git

嗯。。。。怎么说呢,仍是挺励志的。 那就是他了,npm

这个网站虽然有api可是不能直接爬,估计给参数作了加密,我懒,因此直接用selenium了,省的分析他的请求逻辑,就是效率不高,不过无所谓了,他这里ugc的内容很少,加上cron一分钟一次。 爬虫主要代码,这个页面是预加载的因此须要本身加个滑动功能window.scrollBy(0, 400);json

print('start get')
driver.get('https://www.brainyquote.com/')
for i in list(range(50)):
    driver.execute_script("window.scrollBy(0, 400);")
    time.sleep(2)

html = driver.page_source
sel = Selector(text=html)
quotes = sel.xpath("//a[@title='view quote']")
for quote in quotes:
    save_motto(quote, 'all')

cronapi

1 * * * * /usr/bin/python3 /var/www/cx_motto/spider/moto_spider_list.py

客户端

客户端想了想不必作页面因此就直接用js bin功能了 主要代码,很简单。服务器

const options = {
  url: 'http://服务器地址/get_one',
  headers: {
    'User-Agent': 'request'
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    const info = JSON.parse(body);
    console.log(info.data.content)
  }
}

服务端主要代码

服务端使用的lumen想一想访问量不会太大哈~dom

public function getOne(){
        $count = DB::collection('mottos')->count();
        $skip=random_int(0, $count-1);
        $oneItem = DB::collection('mottos')->skip($skip)->first();
        return response()->json(['data'=>$oneItem]);
    }

打个包上传到npmjs

个人叫omotto,像我同样懒得能够直接用个人。ide

npm i omotto -g

运行效果

开放代码~

但愿能帮到你们工具

https://gitee.com/sharpcx/omotto

star

相关文章
相关标签/搜索