背景
有個朋友計劃拓展業務渠道,準備在衆包平臺上接單,他的主營產品是微信小程序,因此他想第一時間收到客戶發出的需求信息,然後第一時間聯繫客戶,這樣成交率才能夠得到保障,否則單早都被其他同行接完了,他的黃花菜也就都涼了。
開發環境
- 開發語言 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'[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
本文章僅供技術交流,不得商用,不得轉載,違者必究。