# 配置参数
config = {
'keyword': 'iphone8', # 搜索关键词
'check_interval': 300, # 检查间隔(秒)
'database': 'xianyu.db', # 数据库文件
'dingtalk_webhook': 'https://oapi.dingtalk.com/robot/send?access_token=your_token'
}
def init_db():
"""初始化数据库"""
conn = sqlite3.connect(config['database'])
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS items
(item_id TEXT PRIMARY KEY, title TEXT, price TEXT, time TEXT)''')
conn.commit()
conn.close()
def check_new_items():
"""检查新商品"""
url = f'https://s.2.taobao.com/list/list.htm?q={config["keyword"]}&search_type=item'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
try:
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
items = soup.find_all('div', class_='item-info')
new_items = []
conn = sqlite3.connect(config['database'])
for item in items:
item_id = item.find('a')['href'].split('=')[-1]
title = item.find('h4').text.strip()
price = item.find('span', class_='price').text.strip()
time = item.find('span', class_='time').text.strip()
# 检查是否已存在
c = conn.cursor()
c.execute("SELECT 1 FROM items WHERE item_id=?", (item_id,))
if not c.fetchone():
new_items.append({
'title': title,
'price': price,
'time': time,
'url': f'https://2.taobao.com/item.htm?id={item_id}'
})
# 存入数据库
c.execute("INSERT INTO items VALUES (?,?,?,?)",
(item_id, title, price, time))
conn.commit()
conn.close()
return new_items
except Exception as e:
print(f'抓取失败: {str(e)}')
return []
def send_dingtalk(msg):
"""发送钉钉通知"""
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "markdown",
"markdown": {
"title": "闲鱼新商品通知",
"text": f"**新商品上架**\n\n{msg}\n"
}
}
try:
requests.post(config['dingtalk_webhook'],
data=json.dumps(data),
headers=headers)
except Exception as e:
print(f'钉钉推送失败: {str(e)}')
def main():
init_db()
while True:
print(f'[{time.ctime()}] 开始检查新商品...')
new_items = check_new_items()
if new_items:
for item in new_items:
message = f"标题:{item['title']}\n价格:{item['price']}\n时间:{item['time']}\n链接:{item['url']}"
send_dingtalk(message)
print(f"发现新商品: {item['title']}")
time.sleep(config['check_interval'])
if __name__ == '__main__':
main()
2025闲鱼监控软件的原理是什么?新人如何学习?
最新推荐文章于 2025-05-14 13:07:11 发布