背景
有個朋友計劃拓展業務渠道,準備在衆包平臺上接單,他的主營產品是微信小程序,因此他想第一時間收到客戶發出的需求信息,然後第一時間聯繫客戶,這樣成交率才能夠得到保障,否則單早都被其他同行接完了,他的黃花菜也就都涼了。
開發環境
- 開發語言 Python ,開發架構Scrapy,非 Python 莫屬,數據採集的神器!
- 開發工具 PyCharm;
功能設計
- 實時通知:採用發郵件方式通知,將郵箱綁定到微信,實現實時通知的效果。
- 過濾模塊:根據標題和內容雙重過濾關鍵詞,不符合要求的訂單丟棄,符合要求的訂單實時通知。
- 配置模塊:採用json文件配置。
關鍵代碼
- 採集模塊
# -*- coding: utf-8 -*-
import time
import scrapy
from scrapy import Selector
from .. import common
class YpwktaskSpider(scrapy.Spider):
name = 'ypwktask'
allowed_domains = ['ypwk.com']
start_urls = ['https://www.epwk.com/task/develop/']
def parse(self, response):
# pass
# price = response.xpath('//h3[@class="a2"]/b/text()').getall()
# name= response.xpath('//h3[@class="a2"]//@indus_pname').getall()
# desc = response.xpath('//h3[@class="a2"]//@desc').getall()
# url = response.xpath('//h3[@class="a2"]//@url').getall()
nodes = response.xpath('//h3[@class="a2"]').getall()
for node in nodes:
# print(node)
price = Selector(text=node).xpath('//b/text()').get().replace(u'\xa0', u' ')
name = Selector(text=node).xpath('//@indus_pname').get().replace(u'\xa0', u' ')
desc = Selector(text=node).xpath('//@desc').get().replace(u'\xa0', u' ')
url = Selector(text=node).xpath('//@url').get()
urgent = Selector(text=node).xpath('//span//@title ').get()
if urgent is None:
urgent = ''
id = Selector(text=node).xpath('//@id ').get()
print(name, price, desc, url, urgent, id)
sended_id = common.read_taskid()
if int(id) > sended_id:
subject = "YPWK " + id + " " + name
# content = price + "\n" + desc + "\n" + url + "\n" + urgent + "\n"
content = "%s <p> %s <p> < a href=%s>%s</ a> <p> %s" % (price, desc, url, url, urgent)
if common.send_mail(subject, content):
common.write_taskid(id=int(id))
print("mail: send task <%s> sucess " % id)
else:
print("mail: send task <%s> fail " % id)
else:
print("mail: task is already sended <%s>" % id)
time.sleep(3)
- 通知模塊
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
本文章僅供技術交流,不得商用,不得轉載,違者必究。