In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the itchat module in Python to send Wechat messages to friends regularly". The content in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought to slowly deepen, together to study and learn "how to use the itchat module in Python to regularly send Wechat messages to friends."
Function
Send daily weather, reminders and daily sentences to your girlfriend regularly.
Data source
Every day, like the boss above, it also comes from ONE.
Weather information from SOJSON
Realize the effect
The code describes the directory structure
City_dict.py: city corresponding coding dictionary
Config.yaml: set the timing time, girlfriend Wechat name and other parameters
GFWeather.py: core code
Requirements.txt: libraries to be installed
Run.py: project run class
Core code
GFWeather.py
Class gfweather: headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 ",} # user of girlfriend id bf_wechat_name_uuid =''def _ _ init__ (self): self.city_code, self.start_datetime, self.bf_wechat_name, self.alarm_hour Self.alarm_minute = self.get_init_data () def get_init_data (self):''initialize basic data: return:' 'with open (' config.yaml', 'r') Encoding='utf-8') as f: config = yaml.load (f) city_name = config.get ('city_name'). Strip () start_date = config.get (' start_date'). Strip () wechat_name = config.get ('wechat_name'). Strip () alarm_timed = config.get (' alarm_timed'). Strip () init_msg = f "regular sending time every day: {alarm_timed}\ n girlfriend's city name : {city_name}\ ngirlfriend's Wechat nickname: {wechat_name}\ nfirst day together date: {start_date} "print (u" * "* 50) print (init_msg) # get the city number according to the city name Used to inquire about the weather. Check the supported cities: http://cdn.sojson.com/_city.json city_code = city_dict.city_dict.get (city_name) if not city_code: print ('your output city cannot receive weather information') start_datetime = datetime.strptime (start_date, "% Y-%m-%d") hour, minute = [int (x) for x in alarm_timed.split (':')] # print (hour, minute) return city_code, start_datetime Wechat_name, hour, minute def is_online (self, auto_login=False):''to determine whether it is still online,: param auto_login:True, and log in automatically if it is disconnected. : return: True, still online, False is not online''def online ():' 'determine whether the user is still online by getting friend information: return: True, still online, False is not online' 'try: if itchat.search_friends (): return True except: return False return True if online (): return True # just judge whether online if not auto_login: return online () # login Try for _ in range (5): # Command line displays login QR code # itchat.auto_login (enableCmdQR=True) itchat.auto_login () if online (): print ('login succeeded') return True else: return False def run (self): # automatic login if not self.is_online (auto_login=True): return # scheduled task scheduler = BlockingScheduler () # send a daily sentence to your girlfriend around 9:30 every day Scheduler.add_job (self.start_today_info 'cron', hour=self.alarm_hour, minute=self.alarm_minute) scheduler.start () def start_today_info (self): print ("*" * 50) print (' get relevant information') Dictum_msg = self.get_dictum_info () today_msg = self.get_weather_info (dictum_msg) print (f' what to send:\ n {today_msg}') if self.is_online (auto_login=True): # get a friend username if not self.bf_wechat_name_uuid: friends = itchat.search_friends (name=self.bf_wechat_name) if not friends: print ('nickname error') return self.bf_wechat_name_uuid = friends [0] .get ('UserName') itchat.send (today_msg ToUserName=self.bf_wechat_name_uuid) print ('send successfully..\ n') def get_dictum_info (self):' 'get the motto information (from "one". One "get information 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') # the daily sentence every_msg = soup_texts.find_all ('div', class_='fp-one-cita') [0]. Find (' a'). Text return every_msg def get_weather_info (self, dictum_msg=''):''to get weather information. Web site: https://www.sojson.com/blog/305.html: param dictum_msg: message to friends: return:''print (' get weather information..') Weather_url = f 'http://t.weather.sojson.com/api/weather/city/{self.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] locale.setlocale (locale.LC_CTYPE 'chinese') today_time = datetime.now (). Strftime (' "% Y% m / d% H:%M:%S") # Weather Note today notice = today_weather.get ('notice') # temperature high = today_weather.get (' high') high_c = high [high.find (') + 1:] low = today_weather.get ('low') low_c = low [low.find ( '') + 1:] temperature = f "temperature: {low_c} / {high_c}" # Wind fx = today_weather.get ('fx') fl = today_weather.get (' fl') wind = f "{fx}: {fl}" # Air Index aqi = today_weather.get ('aqi') aqi = f "Air: {aqi}" day_delta = (datetime.now ()-self.start_datetime ). Days delta_msg = f 'Baby this is our {day_delta} day' today_msg = f'{today_time}\ n {delta_msg}. \ n {notice}\ n {temperature}\ n {wind}\ n {aqi}\ n {dictum_msg}\ nfrom me who loves you most.' Return today_msg project runs installation dependencies
Install all dependencies using pip install-r requirements.txt
Parameter configuration
Config.yaml
# time to send at a fixed time every day, such as: 8:30alarm_timed: '9wechat_name 30 minutes # name of your girlfriend's city city_name:' Guilin'# your girlfriend's Wechat name wechat_name: 'classical' # start_date: '2017-11-11' Thank you for your reading. This is the content of "how to send Wechat messages to your friends regularly with itchat module in Python". After the study of this article, I believe you have a deeper understanding of how to use the itchat module in Python to regularly send Wechat messages to friends, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.