2025闲鱼监控软件的原理是什么?新人如何学习?



# 配置参数
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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值