Project Introduction:

inspiration

After seeing an article in Nuggets “Use Node+wechaty to write a crawler script to send female (male) friends warm words on wechat every day”, I thought why not use Python to implement this function. B: JUST TO IT. The structure of this article also refers to the above friend. Originally just write a single person, but some excellent (make) show (die) people say girlfriend more than one. Support for adding multiplayer information is now available.

Project Address:

Making: github.com/sfyc23/Ever… .

Use the library

  • Itchat – wechat personal number interface
  • Requests – A library of network requests
  • Beautifulsoup4 – Parses web pages
  • APScheduler – Scheduled tasks

function

Regularly send your girlfriend daily weather, reminders and a sentence of the day.

The data source

  • ONE is the same as the other
  • The weather information comes from SOJSON

Implementation effect


Code instructions

The directory structure

  • City_dict. py: code dictionary for cities
  • Config. yaml: Set parameters such as timing time and girlfriend’S wechat name
  • Gfweather.py: Core code
  • Requirements. TXT: library to install
  • Run.py: project run class

The core code

1. Scheduled tasks.

Start sending messages to girlfriends at 9:30 every day.

# Scheduled task
scheduler = BlockingScheduler()
Send your girlfriend one sentence of the day at 9:30 every day
# scheduler.add_job(start_today_info, 'cron', hour=9, minute=30)
scheduler.start()
Copy the code

Start_today_info is the method handling class.

Get one sentence a day.

Data source: ONE

def get_dictum_info(self):
    ' ''Get motto information (from' one.one 'http://wufazhuce.com/) :return: STR a motto or phrase'' '
    print('Get motto information.. ')
    user_url = 'http://wufazhuce.com/'
    resp = requests.get(user_url, headers=self.headers)
    soup_texts = BeautifulSoup(resp.text, 'lxml')
    Sentence of the day in # one-one-one
    every_msg = soup_texts.find_all('div', class_='fp-one-cita')[0].find('a').text
    return every_msg
Copy the code

3. Get today’s weather.

Weather data source: 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's weather
        today_weather = weatherJson.get('data').get('forecast') [1]Copy the code

City_code City ID. cdn.sojson.com/_city.json

4. Log in to wechat and send the content.

itchat.auto_login()
itchat.send(today_msg, toUserName=name_uuid)
Copy the code

Project running

Install dependencies

Install all dependencies using PIP install -r requirements.txt

Parameter configuration

config.yaml

alarm_timed: '9:30'
girlfriend_infos:
  -
    # Girlfriend wechat nickname
    wechat_name: 'classical'
    # Girlfriend lives in Guilin
    city_name: 'the guilin'
    # Started hooking up that day
    start_date: '2017-11-11'
    # The last message for short sentences
    sweet_words: 'From me who loves you most. '

  # If you have more than one girlfriend to send, use this style and copy it
  -
    wechat_name: 'Miss Chen'
    city_name: 'Chaoyang district'
    start_date: '2018-11-11'
    sweet_words: 'From your beautiful husband. '
Copy the code

Began to run

python run.py
Copy the code

The last

Project address: github.com/sfyc23/Ever… After writing, I found that I did not have a girlfriend!