网页内容实时推送钉钉群,创建了数据库,爬取网页内容存储到数据库,判断内容内容是否存在,不存在推送到钉钉群,存在就过滤
sql = "select * from notice_info where title = '" + self.concent["title"] + "' and t = '" + concent["t"] + "';"
AttributeError: 'Concent' object has no attribute 'concent'
sql语句不会,存储数据和判断数据
,有两个类方法,爬取数据储存数据(发送方),接受数据(接收方),在线推送,脚本运行不会报错
import datetime
import random
import sqlite3
import time
import json
import hashlib
import base64
import hmac
import os
import time
import requests
from urllib.parse import quote_plus
import self
from lxml import etree
class Concent:
def init_db(self,dbpath):
sql = '''
create table if not exists 'notice_info'
(
title text,
date text
)
'''
self.dbpath = dbpath
self.connect = sqlite3.connect(dbpath) # 打开或创建数据库
c = self.connect.cursor() # 获取游标
c.execute(sql) # 执行SQL语句
self.connect.commit() # 提交事务
self.connect.close() # 关闭
def dataExtarct(self):
req = requests.get('https://www.jiucaigongshe.com/study_publish')
html = etree.HTML(req.text)
t = html.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/ul/li[5]/div/section/div[1]/div/a/div[2]/div[2]/text()')[0]
title = html.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/ul/li[5]/div/section/div[2]/div/div/text()')[0]
href = 'https://www.jiucaigongshe.com' + html.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/ul/li[5]/div/section/div[3]/div/section/div/div/a/@href')[0]
self.concent = {
"t" : html.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/ul/li[5]/div/section/div[1]/div/a/div[2]/div[2]/text()')[0],
"title" : html.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/ul/li[5]/div/section/div[2]/div/div/text()')[0],
"href" : 'https://www.jiucaigongshe.com' + html.xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/ul/li[5]/div/section/div[3]/div/section/div/div/a/@href')[0]
}
def insert_data(self):
sql = "insert into notice_info(title, t) values('" + self.concent["title"] + "', '" + self.concent["t"] + "');"
c = self.connect.cursor()
c.execute(sql)
self.connect.commit() # 提交事务
self.connect.close() # 关闭
self.save_data()
def save_data(self):
sql = "select * from notice_info where title = '" + self.concent["title"] + "' and t = '" + concent["t"] + "';"
print(sql)
self.connect = sqlite3.connect(self.dbpath) # 打开或创建数据库
c = self.connect.cursor() # 获取游标
cursor = c.execute(sql) # 执行SQL语句
result = 0
for row in cursor:
print(row)
result += 1
if result == 0:
self.insert_data()
return 1
else:
return 0
class Messenger:
def __init__(self, token=os.getenv("DD_ACCESS_TOKEN"), secret=os.getenv("DD_SECRET")):
self.timestamp = str(round(time.time() * 1000))
self.URL = "https://oapi.dingtalk.com/robot/send"
self.headers = {'Content-Type': 'application/json'}
secret = secret
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(self.timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
self.sign = quote_plus(base64.b64encode(hmac_code))
self.params = {'access_token': token, "sign": self.sign}
def send_text(self, content):
"""
发送文本
@param content: str, 文本内容
"""
data = {"msgtype": "text", "text": {"content": content}}
self.params["timestamp"] = self.timestamp
return requests.post(
url=self.URL,
data=json.dumps(data),
params=self.params,
headers=self.headers
)
while True:
concent = Concent()
concent.save_data()
m = Messenger(
token="e2ddba004b474ffae7087f4ddea27fb2455a0f55f78c4f63b62c35a9ef51d026",
secret="SECd072c9f0c2c0d8103c36ef3481162c6256f581abbafbfd4d5683ad224a9f4a88"
)
m.send_text(concent)
照着修改一下你的代码。sql语句也有问题,创建表的字段和你后面插入、查询等字段都不匹配了。
import sqlite3
class Connect:
def __init__(self, dbpath):
self.dbpath = dbpath
self.connect = sqlite3.connect(self.dbpath)
def init_db(self):
sql = '''
create table if not exists 'notice_info'
(
title text,
date text
)
'''
#self.dbpath = dbpath
#self.connect = sqlite3.connect(self.dbpath) # 打开或创建数据库
c = self.connect.cursor() # 获取游标
c.execute(sql) # 执行SQL语句
self.connect.commit() # 提交事务
#self.connect.close() # 关闭
def insert_data(self):
sql = "insert into notice_info(title, date) values('title', 'messages');"
c = self.connect.cursor()
c.execute(sql)
self.connect.commit() # 提交事务
#self.connect.close() # 关闭
self.save_data()
def save_data(self):
sql = "select * from notice_info where title = 'title' and date = 'messages';"
print(sql)
self.connect = sqlite3.connect(self.dbpath) # 打开或创建数据库
c = self.connect.cursor() # 获取游标
cursor = c.execute(sql) # 执行SQL语句
result = 0
for row in cursor:
print(row)
result += 1
if result == 0:
self.insert_data()
return 1
else:
return 0
connect = Connect('mydata.sqlite')
connect.init_db()
#connect.insert_data()
connect.save_data()
你的self.concent是在函数dataExtarct中赋的值,在调用concent.save_data()之前,你没有执行dataExtarct,所以找不到self.concent
同理,也没有执行init_db生成表,也没有执行insert_data插入数据,查询自然也会出错。
concent = Concent()
concent.save_data()
按逻辑顺序应改成:
concent = Concent()
concent.init_db(sqlite的文件路径) #先生成表
concent.dataExtarct() #给字段赋值
concent.save_data() #存数据
AttributeError: 'Concent' object has no attribute 'concent' —— 系统获取不到你自定义的这个类的某个属性
检查变量声明及初始化
检查有没有手误引起的函数拼错
检查引用的模块函数,是不是更新新的版本了