美文网首页Python小哥哥Python学习资料整理思科DevNet
用 Python + itchat 写一个爬虫脚本每天定时给女朋

用 Python + itchat 写一个爬虫脚本每天定时给女朋

作者: 我爱学python | 来源:发表于2019-03-15 14:47 被阅读3次

    项目介绍:

    灵感来源

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

    项目地址:

    Github: github.com/sfyc23/Ever…

    使用库

    功能

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

    数据来源

    实现效果

    image
    image

    代码说明

    目录结构

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

    核心代码

    1.定时任务。

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

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

    start_today_info 是方法处理类。

    2.获取每日一句。

    数据来源: ONE●一个

    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. 获取今日天气 。

    天气数据来源:SOJSON

    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: '来自你俊美的老公。'
    #需要源码加群:984632579获取
    

    开始运行

    python run.py
    复制代码
    

    最后

    写完后才发现,我并没有女朋友啊!

    相关文章

      网友评论

        本文标题:用 Python + itchat 写一个爬虫脚本每天定时给女朋

        本文链接:https://www.haomeiwen.com/subject/lwilmqtx.html