1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
import re import time import pymysql import requests from gxt_spider import get_industry from kjt_spider import get_sci_kjt from sdszf_spider import get_sci_sdszf from jinja2 import Template import json
def connect_to_database(): connection = pymysql.connect( host='127.0.0.1', user='root', password='123456', database='my_database_test', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor ) return connection
def query_today_kc_enterprises(): keywords = [ "科技型中小企业", "高新技术企业", "众创空间", "科技领军企业", "技术先进型服务企业", "技术创新示范企业", "专精特新", "科技企业", "瞪羚", "独角兽", "科技小巨人企业", '小巨人'] not_contain_keywords = ["取消","组织申报","认定和复核","申报","补助名单","绩效评价"] sql = build_sql_query(keywords, not_contain_keywords)
connection = connect_to_database() try: with connection.cursor() as cursor: cursor.execute(sql) results = cursor.fetchall()
return { "total": len(results), "list": results } finally: connection.close()
def build_sql_query(keywords, not_contain_keywords): like_conditions = " OR ".join([f"title LIKE '%{keyword}%'" for keyword in keywords]) not_like_conditions = " and ".join([f"title NOT LIKE '%{not_contain_keyword}%'" for not_contain_keyword in not_contain_keywords]) sql = f""" SELECT CASE type WHEN '1' THEN '山东省科学技术厅' WHEN '2' THEN '山东省工业和技术化厅' WHEN '3' THEN '山东省人民政府' ELSE '未知类型' END AS type_name,date,title,url FROM `sci_spider` WHERE ({like_conditions}) AND ({not_like_conditions}) AND DATE(create_date) = DATE(NOW()) """ return sql
def mail_sender(content): import smtplib from email.mime.text import MIMEText from email.header import Header mail_host = "smtp.163.com" mail_user = "18631839859@163.com" mail_pass = "GENGs7dM45TJDH6y" sender = '18631839859@163.com' receivers = ['wonder1999@126.com']
message = MIMEText(content, 'html', 'utf-8') message['From'] = Header("科技型中小企业通知", 'utf-8') message['To'] = Header("科技型中小企业", 'utf-8')
subject = '科技型中小企业通知' message['Subject'] = Header(subject, 'utf-8')
try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")
def wx_web_hook(data): """ 通过企业微信Webhook发送Markdown格式的消息 :param data: 包含通知数据的字典,结构应包含'total'和'list'键 :return: None """ webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ef84945d-2247-4f09-ac0b-be7a6607c24e"
content = f"**找到 {data['total']} 条疑似符合条件的记录:**\n" for row in data['list']: content += ( f"- [{row['title']}]({row['url']}) " f"<font color=\"comment\">{row['date']}</font> " f"<font color=\"warning\">{row['type_name']}</font>\n" )
payload = { "msgtype": "markdown", "markdown": { "content": content } } try: response = requests.post(webhook_url, json=payload) response.raise_for_status() result = response.json()
if result.get("errcode") == 0: print("✅ 消息发送成功") else: print(f"❌ 消息发送失败: {result.get('errmsg')}")
except requests.exceptions.RequestException as e: print(f"⚠️ 请求异常: {e}")
if __name__ == '__main__': get_industry(1, 2) get_sci_kjt(1, 1) get_sci_sdszf(1, 3) data = query_today_kc_enterprises() title = f"找到 {data['total']} 条疑似符合条件的记录:" for row in data['list']: print(row)
if data['total'] > 0: wx_web_hook(data)
|