用 Python + Itchat 写一个爬虫脚本天天定时给女友发微信暖心话

项目介绍:

灵感来源

在掘金看到了一篇《用Node+wechaty写一个爬虫脚本天天定时给女(男)朋友发微信暖心话》后,我就想为何不用 Python 去实现这个功能呢。 JUST TO IT,说作就作。
这文章的结构也是参考上面这位朋友的。
原本只是写单人的,不过有些优(做)秀(死)的人表示女友不止一个。现已支持添加多人信息。html

项目地址:

Github: github.com/sfyc23/Ever…python

使用库

功能

定时给女友发送每日天气、提醒、每日一句。git

数据来源

实现效果

命令行信息

微信截图

代码说明

目录结构

  • city_dict.py :城市对应编码字典
  • config.yaml :设置定时时间,女朋友微信名称等参数
  • GFWeather.py:核心代码
  • requirements.txt:须要安装的库
  • run.py:项目运行类

核心代码

1.定时任务。

天天 9:30 给女友们开始给女友发送内容。github

# 定时任务
scheduler = BlockingScheduler()
# 天天9:30给女友发送每日一句
# scheduler.add_job(start_today_info, 'cron', hour=9, minute=30)
scheduler.start()
复制代码

start_today_info 是方法处理类。json

2.获取每日一句。

数据来源: ONE●一个api

def get_dictum_info(self):
    ''' 获取格言信息(从『一个。one』获取信息 http://wufazhuce.com/) :return: str 一句格言或者短语 '''
    print('获取格言信息..')
    user_url = 'http://wufazhuce.com/'
    resp = requests.get(user_url, headers=self.headers)
    soup_texts = BeautifulSoup(resp.text, 'lxml')
    # 『one -个』 中的每日一句
    every_msg = soup_texts.find_all('div', class_='fp-one-cita')[0].find('a').text
    return every_msg
复制代码

3. 获取今日天气 。

天气数据来源:SOJSONbash

def get_weather_info(self, city_code=''):
    weather_url = f'http://t.weather.sojson.com/api/weather/city/{city_code}'
    resp = requests.get(url=weather_url)
    if resp.status_code == 200 and resp.json().get('status') == 200:
        weatherJson = resp.json()
        # 今日天气
        today_weather = weatherJson.get('data').get('forecast')[1]
复制代码

city_code 城市对应 id。 cdn.sojson.com/_city.json微信

4. 登陆微信并发送内容。

itchat.auto_login()
itchat.send(today_msg, toUserName=name_uuid)
复制代码

项目运行

安装依赖

使用 pip install -r requirements.txt 安装全部依赖网络

参数配置

config.yaml并发

alarm_timed: '9:30'
girlfriend_infos:
  -
    #女朋友微信昵称
    wechat_name: '古典'
    #女朋友所在桂林
    city_name: '桂林'
    # 从那天开始勾搭的
    start_date: '2017-11-11'
    # 短句的最后留言
    sweet_words: '来自最爱你的我。'

  #若是你有多个女朋友须要发送,则参照这个样式,复制便可
  -
    wechat_name: '陈老师'
    city_name: '朝阳区'
    start_date: '2018-11-11'
    sweet_words: '来自你俊美的老公。'
复制代码

开始运行

python run.py
复制代码

最后

项目地址:github.com/sfyc23/Ever… 写完后才发现,我并无女友啊!

相关文章
相关标签/搜索