当前位置: 移动技术网 > IT编程>脚本编程>Python > 数据采集 - 获取【oschina】最新发布需求,并实时通知用户 案例三

数据采集 - 获取【oschina】最新发布需求,并实时通知用户 案例三

2020年07月07日  | 移动技术网IT编程  | 我要评论
背景有个朋友计划拓展业务渠道,准备在众包平台上接单,他的主营产品是微信小程序,因此他想第一时间收到客户发出的需求信息,然后第一时间联系客户,这样成交率才能够得到保障,否则单早都被其他同行接完了,他的黄花菜也就都凉了。开发环境开发语言 Python ,开发架构Scrapy,非 Python 莫属,数据采集的神器!开发工具 PyCharm;功能设计实时通知:采用发邮件方式通知,将邮箱绑定到微信,实现实时通知的效果。过滤模块:根据标题和内容双重过滤关键词,不符合要求的订单丢弃,符合要求的

背景

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

开发环境

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

功能设计

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

关键代码

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

import scrapy
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from scrapy import Selector
from . import common


class OschinataskSpider(scrapy.Spider):
    name = 'oschinaTask'
    domain = "https://zb.oschina.net"
    allowed_domains = [domain]
    start_url = 'https://zb.oschina.net/projects/list.html'
    start_urls = [start_url]

    def __init__(self):
        options = Options()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--disable-gpu')
        options = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(options=options)
        # self.driver.set_page_load_timeout(60)

    def close(self, spider, reason):
        self.driver.quit()

    def start_requests(self):
        # dont_filter=True Filtered duplicate request    no more duplicates will be shown (see DUPEFILTER_DEBUG to show all duplicates)
        yield scrapy.Request(self.start_url, callback=self.parse, dont_filter=True)


    def parse(self, response):
        print("parse ")
        nodes = response.xpath('//div[@class="el-col el-col-19"]').getall()
        sended_id = common.read_taskid()
        max_id = sended_id
        for node in nodes:
            title = Selector(text=node).xpath('//span[@class="title"]/a/text()').get()
            url = self.domain + Selector(text=node).xpath('//span[@class="title"]/a/@href').get()
            pos = url.find("id=")
            id_str = url[pos + 3:]
            id = int(id_str)
            # print(id_str)
            price = Selector(text=node).xpath('//span[@class="money"]/text()').get()
            skills = Selector(text=node).xpath('//span[@class="skills"]/text()').get()
            tags = Selector(text=node).xpath('//div[@class="tags mb-4"]/span/text()').getall()
            tag = ""
            for i in tags:
                tag = tag + i + " "
            subject = "oschina " + id_str + " " + title
            content = "%s <p> %s <p> <a href=%s>%s</a>  <p> %s" % (price, skills, url, url, tag)
            # print(content)
            if id > sended_id:
                if id > max_id:
                    max_id = id
                common.send_mail(subject, content)
            else:
                print("mail: task is already sended  <%r>" % id)
            time.sleep(3)
        # end for node in nodes
        # 记录最大id
        common.write_taskid(id=max_id)

        time.sleep(10 * 60)
        # 循环爬取单个url,在这里!!!!!  dont_filter 这个坑坑
        yield scrapy.Request(self.start_url, callback=self.parse, dont_filter=True)
  • 通知模块
 
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

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

本文地址:https://blog.csdn.net/lildkdkdkjf/article/details/107151659

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网