【定制开发】【M6】Python爬虫 - 获取【猪八戒】最新发布需求,并实时通知用户

背景

有个朋友计划拓展业务渠道,准备在众包平台上接单,他的主营产品是微信小程序,因此他想第一时间收到客户发出的需求信息,然后第一时间联系客户,这样成交率才能够得到保障,否则单早都被其他同行接完了,他的黄花菜也就都凉了。

开发环境

  • 开发语言 Python ,开发架构Scrapy,非 Python 莫属,数据采集的神器!
  • 开发工具 PyCharm;

功能设计

  • 实时通知:采用发邮件方式通知,将邮箱绑定到微信,实现实时通知的效果。
  • 过滤模块:根据标题和内容双重过滤关键词,不符合要求的订单丢弃,符合要求的订单实时通知。
  • 配置模块:采用json文件配置。

关键代码

  • 采集模块
# -*- coding: utf-8 -*-
import re
import time

import scrapy
from scrapy import Selector

from .. import common

class ZbjtaskSpider(scrapy.Spider):
    name = 'zbjtask'
    allowed_domains = ['zbj.com']
    start_urls = ['https://task.zbj.com/?m=1111&so=1&ss=0&fee=1']

    def parse(self, response):
        #30 item per page
        nodes = response.xpath('//div[@class="demand-card"]').getall()
        id_nodes = response.xpath('//a[@class="prevent-defalut-link"]/@href').getall()

        print(id_nodes)
        max_id = 0
        for url in id_nodes:
            # //task.zbj.com/16849389/
            pattern = re.compile("/\d*/$")
            id_str_ori = pattern.findall(url).pop()
            id_str = id_str_ori[1:len(id_str_ori) - 1]
            id = int(id_str)
            if id > max_id:
                max_id = id
        print(max_id)

        for node in nodes:
            date = Selector(text=node).xpath('//span[@class="card-pub-time flt"]/text()').get()
            url = "https:" + Selector(text=node).xpath('//a[@class="prevent-defalut-link"]/@href').get()
            name = Selector(text=node).xpath('//a[@class="prevent-defalut-link"]/text()').get()
            desc = Selector(text=node).xpath('//div[@class="demand-card-desc"]/text()').get()
            price = Selector(text=node).xpath('//div[@class="demand-price"]/text()').get()
            tag = Selector(text=node).xpath('//span[@class="demand-tags"]/i/text()').get()

            # //task.zbj.com/16849389/
            pattern = re.compile("/\d*/$")
            id_str_ori = pattern.findall(url).pop()
            id_str = id_str_ori[1:len(id_str_ori)-1]
            id = int(id_str)

            sended_id = common.read_taskid()
            if  id > sended_id :
                subject = "ZBJ " + id_str + " " + name
                # content = price + "\n" + desc + "\n" +  url + "\n" + tag + "\n"
                content = "%s <p> %s <p> <a href=%s>%s</a>  <p> %s" % (price, desc, url, url, tag)
                if common.send_mail(subject, content):
                    print("ZBJ mail: send task <%r> sucess " % id)
                else:
                    print("ZBJ mail: send task <%r> fail " % id)
            else :
                print("mail: task is already sended  <%r>" % id)
            time.sleep(3)

        common.write_taskid(id=max_id)

  • 通知模块

def send_mail(subject, content):
    sender = u'xxxxx@qq.com'  # 发送人邮箱
    passwd = u'xxxxxx'  # 发送人邮箱授权码
    receivers = u'xxxxx@qq.com'  # 收件人邮箱

    # subject = u'一品威客 开发任务 ' #主题
    # content = u'这是我使用python smtplib模块和email模块自动发送的邮件'    #正文
    try:
        # msg = MIMEText(content, 'plain', 'utf-8')
        msg = MIMEText(content, 'html', 'utf-8')
        msg['Subject'] = subject
        msg['From'] = sender
        msg['TO'] = receivers

        s = smtplib.SMTP_SSL('smtp.qq.com', 465)
        s.set_debuglevel(1)
        s.login(sender, passwd)
        s.sendmail(sender, receivers, msg.as_string())
        return True
    except Exception as e:
        print(e)
        return False

总结

程序上线后稳定运行,实现了预期的效果,接单率效果杠杠滴!

附:猪八戒平台架构图

附:Scrapy思维导图

-------------------------------------------------------------------------------------------------------------------

本次分享结束,欢迎讨论!QQ微信同号: 6550523

本文章仅供技术交流,不得商用,不得转载,违者必究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelloCode5110

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值