思路就是,我去爬取星座和新聞信息后,然后登陸到微信瞳脓,發(fā)給好友就可以了
# 導(dǎo)入模塊
#!/usr/bin/python
#-*- coding:utf-8 -*-
import os
import re
import time
import json
import itchat
import requests
from lxml import etree
from urllib.parse import urljoin
from apscheduler.schedulers.blocking import BlockingScheduler
爬取信息以及微信發(fā)送信息模塊:
headers = {
"user-agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36"
}
class SendWXInfos(object):
def __init__(self):
self.lists = [] # 放鏈接
self.xingzuoInfos = [] # 放星座信息
self.wxFriendsname = ["測試用戶1","測試用戶2"] #微信好友昵稱
# 獲取星座鏈接
def getInfos(self):
url = 'http://www.xzw.com/fortune/'#'https://www.d1xz.net/yunshi/today/Pisces/'
res = requests.get(url,headers=headers,timeout=30).text
etr = etree.HTML(res)
lis = etr.xpath('//div[@id="list"]//div[@class="alb"]//dl//dt//a')#('//ul[@id="btn_left"]//li//a')
for li in lis:
# href = urljoin("https://www.d1xz.net" , li.xpath('@href')[0])
href = urljoin("http://www.xzw.com" , li.xpath('@href')[0])
self.lists.append(href)
# 獲取星座名稱
def getXZDet(self,url):
res = requests.get(url,headers=headers,timeout=30).text
etr = etree.HTML(res)
title = etr.xpath('//div[@class="c_main"]//h4')[0].xpath('string(.)').strip()#('//p[@class="title fb"]')[0].xpath('string(.)').strip()#.encode('GBK')
txt = etr.xpath('//div[@class="c_cont"]//p[1]//span')[0].xpath('string(.)').strip()#('//div[@class="txt"]')[0].xpath('string(.)').replace('','')#.strip().encode('GBK')
self.xingzuoInfos.append(title+":\n"+txt+"\n")
aa = title+":\n"+txt+"\n"
if '雙魚座今日運勢' in aa:
print(aa)
# 獲取新聞信息
def getNews(self):
url = 'https://feed.sina.com.cn/api/roll/get'
params = {
"_":round(int(time.time())*1000),
"callback":"feedCardJsonpCallback",
"encode":"utf-8",
"lid":"1356",
"num":"20",
"page":"1",
"pageid":"121",
"versionNumber":"1.2.4"
}
res = requests.get(url,headers=headers,params=params,timeout=30).text
infos = re.findall('feedCardJsonpCallback\((.*)\);\}catch',res)[0].replace('█','')
infos = json.loads(infos)['result']['data']
self.news = ["新聞早報:"] #保存新聞信息
for i,info in enumerate(infos):
self.news.append(str(i+1)+'、'+info['title'])
self.newsmessage = '\n'.join(self.news) #新聞信息
print(self.newsmessage)
# 微信發(fā)信息
def send_news(self):
for wxn in self.wxFriendsname:
try:
# 發(fā)送給朋友
my_friend = itchat.search_friends(name=wxn)
name = my_friend[0]['UserName'] # 0 第一個
except Exception as e:
# 發(fā)送給群聊
myroom = itchat.search_chatrooms(name=wxn)
name = myroom[0]['UserName']
if '測試用戶1' == wxn:
message = ''.join([m for m in self.xingzuoInfos if '巨蟹座今日運勢' in m])
elif wxn == '測試用戶2':
message = ''.join([m for m in self.xingzuoInfos if '天蝎座今日運勢' in m])
else:
print(message)
itchat.send(self.newsmessage,toUserName=name) # 發(fā)送新聞信息
itchat.send(message,toUserName=name) # 發(fā)送星座信息
微信登錄:
itchat.auto_login(hotReload=True)
sche = BlockingScheduler()
time.sleep(2)
sche.add_job(self.send_news,'date',run_date=time.strftime("%Y-%m-%d %H:%M:%S"))
sche.start()
--> github