數據採集 - 獲取【碼市】最新發布需求,並實時通知用戶 案例二

背景

有個朋友計劃拓展業務渠道,準備在衆包平臺上接單,他的主營產品是微信小程序,因此他想第一時間收到客戶發出的需求信息,然後第一時間聯繫客戶,這樣成交率才能夠得到保障,否則單早都被其他同行接完了,他的黃花菜也就都涼了。

開發環境

  • 開發語言 Python ,開發架構Scrapy,非 Python 莫屬,數據採集的神器!
  • 開發工具 PyCharm;

功能設計

  • 實時通知:採用發郵件方式通知,將郵箱綁定到微信,實現實時通知的效果。
  • 過濾模塊:根據標題和內容雙重過濾關鍵詞,不符合要求的訂單丟棄,符合要求的訂單實時通知。
  • 配置模塊:採用json文件配置。

關鍵代碼

  • 採集模塊
# -*- coding: utf-8 -*-
import re

import scrapy
from flask import json
from requests import Request
from scrapy import Selector
from .. import common
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class CodemarttaskSpider(scrapy.Spider):
    name = 'codemarttask'
    allowed_domains = ['codemart.com']
    start_urls = ['https://codemart.com/api/project?page=1&roleTypeId=&status=RECRUITING']

    # 重要,需要修改 application/json ,否則默認返回的是xml數據!!!
    def parse(self, response):
        # 30 item per page
        # print(response.text)
        print(
            "--------------------------------------------------------------------------------------------------------")
        json_data = json.loads(response.text)
        rewards = json_data.get("rewards")
        print(rewards)
        url_prefix = "https://codemart.com/project/"

        sended_id = common.read_taskid()
        max_id = sended_id
        print("sended_id ", sended_id)
        for node in rewards:
            id = node.get("id")
            id_str = str(id)
            name = node.get("name")
            description = node.get("description")
            price = node.get("price")
            roles = node.get("roles")  # 招募:【roles】
            status = node.get("status")
            pubTime = node.get("pubTime")
            url = url_prefix + id_str
            print(name)
            print(pubTime)
            print(price)

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

        # 記錄最大id
        common.write_taskid(id=max_id)
  • 通知模塊
 
def send_mail(subject, content):
    sender = u'[email protected]'  # 發送人郵箱
    passwd = u'xxxxxx'  # 發送人郵箱授權碼
    receivers = u'[email protected]'  # 收件人郵箱
 
    # 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

本文章僅供技術交流,不得商用,不得轉載,違者必究。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章