问题遇到的现象和发生背景
网页内容实时推送钉钉群,创建了数据库,爬取网页内容存储到数据库,判断内容内容是否存在,不存在推送到钉钉群,存在就过滤
问题相关代码,请勿粘贴截图
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)