python爬虫*案例,并自动发微信

import requests
from bs4 import BeautifulSoup
import itchat
import time,datetime

all = [{1, 2, 3, 7, 8},
{1, 2, 3, 7, 9},
{1, 2, 3, 7, 10},
{1, 2, 3, 8, 9},
{1, 2, 3, 8, 10},
{1, 2, 3, 9, 10},
{1, 2, 4, 7, 8},
{1, 2, 4, 7, 9},
{1, 2, 4, 7, 10},
{1, 2, 4, 8, 9},
{1, 2, 4, 8, 10},
{1, 2, 4, 9, 10},
{1, 2, 5, 7, 8},
{1, 2, 5, 7, 9},
{1, 2, 5, 7, 10},
{1, 2, 5, 8, 9},
{1, 2, 5, 8, 10},
{1, 2, 5, 9, 10},
{1, 2, 6, 7, 8},
{1, 2, 6, 7, 9},
{1, 2, 6, 7, 10},
{1, 2, 6, 8, 9},
{1, 2, 6, 8, 10},
{1, 2, 6, 9, 10},
{1, 2, 7, 8, 9},
{1, 2, 7, 8, 10},
{1, 2, 7, 8, 11},
{1, 2, 7, 9, 10},
{1, 2, 7, 9, 11},
{1, 2, 7, 10, 11},
{1, 2, 8, 9, 10},
{1, 2, 8, 9, 11},
{1, 2, 8, 10, 11},
{1, 2, 9, 10, 11},
{1, 3, 4, 7, 8},
{1, 3, 4, 7, 9},
{1, 3, 4, 7, 10},
{1, 3, 4, 8, 9},
{1, 3, 4, 8, 10},
{1, 3, 4, 9, 10},
{1, 3, 5, 7, 8},
{1, 3, 5, 7, 9},
{1, 3, 5, 7, 10},
{1, 3, 5, 8, 9},
{1, 3, 5, 8, 10},
{1, 3, 5, 9, 10},
{1, 3, 6, 7, 8},
{1, 3, 6, 7, 9},
{1, 3, 6, 7, 10},
{1, 3, 6, 8, 9},
{1, 3, 6, 8, 10},
{1, 3, 6, 9, 10},
{1, 3, 7, 8, 9},
{1, 3, 7, 8, 10},
{1, 3, 7, 8, 11},
{1, 3, 7, 9, 10},
{1, 3, 7, 9, 11},
{1, 3, 7, 10, 11},
{1, 3, 8, 9, 10},
{1, 3, 8, 9, 11},
{1, 3, 8, 10, 11},
{1, 3, 9, 10, 11},
{1, 4, 5, 7, 8},
{1, 4, 5, 7, 9},
{1, 4, 5, 7, 10},
{1, 4, 5, 8, 9},
{1, 4, 5, 8, 10},
{1, 4, 5, 9, 10},
{1, 4, 6, 7, 8},
{1, 4, 6, 7, 9},
{1, 4, 6, 7, 10},
{1, 4, 6, 8, 9},
{1, 4, 6, 8, 10},
{1, 4, 6, 9, 10},
{1, 4, 7, 8, 9},
{1, 4, 7, 8, 10},
{1, 4, 7, 8, 11},
{1, 4, 7, 9, 10},
{1, 4, 7, 9, 11},
{1, 4, 7, 10, 11},
{1, 4, 8, 9, 10},
{1, 4, 8, 9, 11},
{1, 4, 8, 10, 11},
{1, 4, 9, 10, 11},
{1, 5, 6, 7, 8},
{1, 5, 6, 7, 9},
{1, 5, 6, 7, 10},
{1, 5, 6, 8, 9},
{1, 5, 6, 8, 10},
{1, 5, 6, 9, 10},
{1, 5, 7, 8, 9},
{1, 5, 7, 8, 10},
{1, 5, 7, 8, 11},
{1, 5, 7, 9, 10},
{1, 5, 7, 9, 11},
{1, 5, 7, 10, 11},
{1, 5, 8, 9, 10},
{1, 5, 8, 9, 11},
{1, 5, 8, 10, 11},
{1, 5, 9, 10, 11},
{2, 3, 4, 7, 8},
{2, 3, 4, 7, 9},
{2, 3, 4, 7, 10},
{2, 3, 4, 8, 9},
{2, 3, 4, 8, 10},
{2, 3, 4, 9, 10},
{2, 3, 5, 7, 8},
{2, 3, 5, 7, 9},
{2, 3, 5, 7, 10},
{2, 3, 5, 8, 9},
{2, 3, 5, 8, 10},
{2, 3, 5, 9, 10},
{2, 3, 6, 7, 8},
{2, 3, 6, 7, 9},
{2, 3, 6, 7, 10},
{2, 3, 6, 8, 9},
{2, 3, 6, 8, 10},
{2, 3, 6, 9, 10},
{2, 3, 7, 8, 9},
{2, 3, 7, 8, 10},
{2, 3, 7, 8, 11},
{2, 3, 7, 9, 10},
{2, 3, 7, 9, 11},
{2, 3, 7, 10, 11},
{2, 3, 8, 9, 10},
{2, 3, 8, 9, 11},
{2, 3, 8, 10, 11},
{2, 3, 9, 10, 11},
{2, 4, 5, 7, 8},
{2, 4, 5, 7, 9},
{2, 4, 5, 7, 10},
{2, 4, 5, 8, 9},
{2, 4, 5, 8, 10},
{2, 4, 5, 9, 10},
{2, 4, 6, 7, 8},
{2, 4, 6, 7, 9},
{2, 4, 6, 7, 10},
{2, 4, 6, 8, 9},
{2, 4, 6, 8, 10},
{2, 4, 6, 9, 10},
{2, 4, 7, 8, 9},
{2, 4, 7, 8, 10},
{2, 4, 7, 8, 11},
{2, 4, 7, 9, 10},
{2, 4, 7, 9, 11},
{2, 4, 7, 10, 11},
{2, 4, 8, 9, 10},
{2, 4, 8, 9, 11},
{2, 4, 8, 10, 11},
{2, 4, 9, 10, 11},
{2, 5, 6, 7, 8},
{2, 5, 6, 7, 9},
{2, 5, 6, 7, 10},
{2, 5, 6, 8, 9},
{2, 5, 6, 8, 10},
{2, 5, 6, 9, 10},
{2, 5, 7, 8, 9},
{2, 5, 7, 8, 10},
{2, 5, 7, 8, 11},
{2, 5, 7, 9, 10},
{2, 5, 7, 9, 11},
{2, 5, 7, 10, 11},
{2, 5, 8, 9, 10},
{2, 5, 8, 9, 11},
{2, 5, 8, 10, 11},
{2, 5, 9, 10, 11},
{3, 4, 5, 7, 8},
{3, 4, 5, 7, 9},
{3, 4, 5, 7, 10},
{3, 4, 5, 8, 9},
{3, 4, 5, 8, 10},
{3, 4, 5, 9, 10},
{3, 4, 6, 7, 8},
{3, 4, 6, 7, 9},
{3, 4, 6, 7, 10},
{3, 4, 6, 8, 9},
{3, 4, 6, 8, 10},
{3, 4, 6, 9, 10},
{3, 4, 7, 8, 9},
{3, 4, 7, 8, 10},
{3, 4, 7, 8, 11},
{3, 4, 7, 9, 10},
{3, 4, 7, 9, 11},
{3, 4, 7, 10, 11},
{3, 4, 8, 9, 10},
{3, 4, 8, 9, 11},
{3, 4, 8, 10, 11},
{3, 4, 9, 10, 11},
{3, 5, 6, 7, 8},
{3, 5, 6, 7, 9},
{3, 5, 6, 7, 10},
{3, 5, 6, 8, 9},
{3, 5, 6, 8, 10},
{3, 5, 6, 9, 10},
{3, 5, 7, 8, 9},
{3, 5, 7, 8, 10},
{3, 5, 7, 8, 11},
{3, 5, 7, 9, 10},
{3, 5, 7, 9, 11},
{3, 5, 7, 10, 11},
{3, 5, 8, 9, 10},
{3, 5, 8, 9, 11},
{3, 5, 8, 10, 11},
{3, 5, 9, 10, 11},
{4, 5, 6, 7, 8},
{4, 5, 6, 7, 9},
{4, 5, 6, 7, 10},
{4, 5, 6, 8, 9},
{4, 5, 6, 8, 10},
{4, 5, 6, 9, 10},
{4, 5, 7, 8, 9},
{4, 5, 7, 8, 10},
{4, 5, 7, 8, 11},
{4, 5, 7, 9, 10},
{4, 5, 7, 9, 11},
{4, 5, 7, 10, 11},
{4, 5, 8, 9, 10},
{4, 5, 8, 9, 11},
{4, 5, 8, 10, 11},
{4, 5, 9, 10, 11},]
ret_list=[
          'http://chart.icaile.com/gd11x5.php',
          'http://chart.icaile.com/',
          'http://chart.icaile.com/jx11x5.php',
          'http://chart.icaile.com/sh11x5.php',
          'http://chart.icaile.com/js11x5.php',
          'http://chart.icaile.com/yn11x5.php',
          'http://chart.icaile.com/jl11x5.php',
          'http://chart.icaile.com/nmg11x5.php',
          'http://chart.icaile.com/ah11x5.php',
          ]
while True:
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    #和值大小
    for i in ret_list:  #循环9个省份
        ret = requests.get(i)  #爬数据
        ret1 = ret.text     #输出内容
        soup = BeautifulSoup(ret1,'html.parser')    #beautifulsoup解析
        title = soup.title      #获取title
        periods = soup.find_all("td","chart-bg-qh")         #找到所有期数
        result = []     #去除所有和值
        for period in periods:
            period_text = int(period.get_text())     #每期期数
            sum = int(period.find_next_siblings("td")[21].get_text())   #找到和值
            last = sum % 10     #尾数
            result.append(last)     #将尾数加入空列表
        result.reverse()        #反转list,让最新的盘在前面
        period_last = int(period.get_text())
        result_big=[]
        result_sml=[]
        #判断大
        for i in result:
            if i > 4 :
                result_big.append(i)
            else:break
        len_big = len(result_big)   #查看大的有几期
        print("%s %s ---大 -------%s期" % (title.get_text(), period_last,len_big))
        if len_big > 8:     #满足条件是发微信
            print("33[1;30;41m%s %s ---大 -------%s期33[0m" % (title.get_text(), period_last, len_big))
            try:
                itchat.auto_login(hotReload=True)   #微信登录
                users = itchat.search_friends(name='Mo')    #选择用户
                userName = users[0]['UserName']     #用于发消息+
                itchat.send(title.get_text()+ '----大'+ '-------'+str(len_big)+'' + '----'+ datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') , toUserName=userName)
                time.sleep(1)
            except Exception as e:
                print("微信发消息异常")
                continue
        #判断小
        for i in result:
            if i < 5 :
                result_sml.append(i)
            else:break
        len_sml = len(result_sml)       #查看小的有几期
        print("%s %s ---小 -------%s期" % (title.get_text(), period_last, len_sml))
        if len_sml > 8:     #满足条件是发微信
            print("33[1;30;41m%s %s ---小 -------%s期33[0m" % (title.get_text(), period_last, len_sml))
            try:
                itchat.auto_login(hotReload=True)   #微信登录
                users = itchat.search_friends(name='Mo')    #选择用户
                userName = users[0]['UserName']     #用于发消息+
                itchat.send(title.get_text()+ '----小'+ '--------'+str(len_sml) +''+ '----'+ datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') , toUserName=userName)
                time.sleep(1)
            except Exception as e:
                print("微信发消息异常")
                continue
    print('-------------------------------------------------------------')
    #23
    for i in ret_list:  #循环9个省份
        ret = requests.get(i)  #爬数据
        ret1 = ret.text     #输出内容
        soup = BeautifulSoup(ret1,'html.parser')    #beautifulsoup解析
        title = soup.title      #获取title
        periods = soup.find_all("td","chart-bg-qh")         #找到所有期数
        period = periods[-1].get_text()  #找到最后一期
        four_all = soup.find_all("td","chart-bg-hmfb dqhm")     #找到出奖号码中的4位
        one_all = soup.find_all("td","chart-bg-kjhmo")      #找到出奖号码中的1位
        four_list = []      #所有的4位,加入一个列表
        one_list = []       #所有的1位,加入一个列表
        for i in four_all:
            four = int(i.get_text())
            four_list.append(four)
        for i in one_all:
            one = int(i.get_text())
            one_list.append(one)
        four_list_split = [four_list[i:i+4] for i in range(0,len(four_list),4)] #[[1, 3, 7, 9], [2, 6, 9, 11], [2, 4, 5, 8]]
        one_list_split = [one_list[i:i + 1] for i in range(0, len(one_list),1)]     #[[2], [4], [6], [8], [3]]
        five_list = []      #生成开奖号码用
        five_list_set = []   #转化为集合方式
        five_23_ok = []
        for i,j in zip(four_list_split,one_list_split):     #开奖的五位数 [[1, 5, 6, 9, 7], [3, 4, 5, 9, 7], [2, 8, 9, 11, 4]]
            five = i + j
            five_list.append(five)
        five_list.reverse()     #将最新的排在前面
        for i in five_list :
            five_set = set(i)
            five_list_set.append(five_set)
        # print(five_list_set)        #开奖的五位数 [{8, 9, 11, 4, 5}, {11, 8, 3, 5, 7}, {11, 9, 10, 3, 7}]
        for i in five_list_set:
            if i not in all:
                five_23_ok.append(i)
            else:break
        len23 = len(five_23_ok)
        print("%s %s ---23 -------%s期" % (title.get_text(),period ,len23))
        if len23 > 6:   #满足6期才发信息
            try:
                itchat.auto_login(hotReload=True)  # 微信登录
                users = itchat.search_friends(name='Mo')  # 选择用户
                userName = users[0]['UserName']  # 用于发消息+
                itchat.send(title.get_text() + period +'----23'+'-------'+ str(len23)+'' + '----' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                            toUserName=userName)
                time.sleep(1)
            except Exception as e:
                print("微信发消息异常")
                continue
    print('--------------------------------------------------------------')
    itchat.logout()
    time.sleep(60)
原文地址:https://www.cnblogs.com/wangmo/p/9183825.html