利用Python获取最新的sci论文摘要信息并群发邮箱

利用Python获取最新的sci论文摘要信息并群发邮箱

1. 需要的包:biopython、markdown

2. 正式代码

import re
import smtplib
import sys
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

import markdown
from Bio import Entrez
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QLabel, QFormLayout, QPushButton, QFileDialog, QMessageBox


class xia(QWidget):
    def __init__(self):
        super().__init__()  # 类初始化
        self.resize(1200,200)
        self.QPushButton1 = QPushButton("选择Term文件:")  # 添加按钮1
        self.QLineEdit1 = QLineEdit()  # 添加文本框1

        self.QPushButton2 = QPushButton("邮件接收人员:")  # 添加按钮2
        self.QLineEdit2 = QLineEdit()  # 添加文本框2

        self.QLabel1 = QLabel('邮 件  主 题 :')
        self.QLineEdit3 = QLineEdit()

        self.QPushButton3 = QPushButton("开始发送")  # 添加按钮3

        self.html = ''
        self.receiver = []

        layout = QFormLayout()  # 建立布局
        layout.addRow(self.QPushButton1, self.QLineEdit1)  # 将按钮1和文本框1加入布局
        layout.addRow(self.QPushButton2, self.QLineEdit2)  # 将按钮2和文本框2加入布姆
        layout.addRow(self.QLabel1, self.QLineEdit3)  # 将按钮2和文本框2加入布姆
        layout.addRow(self.QPushButton3)  # 将按钮3加入布局

        self.setLayout(layout)  # 将layout设定为类的布局
        self.QPushButton1.clicked.connect(self.term_Get)
        self.QPushButton2.clicked.connect(self.receiver_Get)
        self.QPushButton3.clicked.connect(self.operation_Run)
        print(self.html)

    @staticmethod  # 通过内部类,设定markdownpad转html
    def md2html(mdstr):
        exts = ['markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.tables',
                'markdown.extensions.toc']

        html = '''
        <html lang="zh-cn">
        <head>
        <meta content="text/html; charset=utf-8" http-equiv="content-type" />
        <link href="default.css" rel="stylesheet">
        <link href="github.css" rel="stylesheet">
        </head>
        <body>
        %s
        </body>
        </html>
        '''
        ret = markdown.markdown(mdstr, extensions=exts)
        return html % ret

    @staticmethod
    def html_Get(keyword):  # 获取
        Entrez.email = 'mlxia@tust.edu.cn'
        # if self.QLineEdit1.text() is None:
        #     reply=QMessageBox.information(self,"状态提醒","Term栏不能为空",QMessageBox.Yes|QMessageBox.No)
        #     return
        # keyword = self.QLineEdit1.text()

        handle = Entrez.esearch(db='pubmed', term=keyword)
        record = Entrez.read(handle)
        result = record['IdList']
        print(result)
        handle2 = Entrez.efetch(db='pubmed', rettype='abstract', id=result, retmode='text')
        html = handle2.read()
        html = markdown.markdown(html)
        return html

    @staticmethod
    def html_Reshape(html):
        html2 = html.replace(html[0:9], '## ')  # 第一级题目
        list1 = [i.start() for i in re.finditer('## ', html2)]
        list2 = [i.start() for i in re.finditer('</li>\n</ol>', html2)]
        list1.reverse()
        list2.reverse()
        for i in range(len(list1)):
            str1 = html2[:list1[i]]
            str2 = html2[list1[i] + 1:list2[i]].replace('\n', ' ') + '\n'
            str3 = html2[list2[i] + 12:]
            html2 = str1 + str2 + str3

        html2 = html2.replace('# ', '## ')
        # ----------------------------------------------------------------------

        page_Num = len(list1)
        html2 = html2.replace('doi:\n', 'doi: ')  # 题目解决完毕
        # ----------------------------------------------------------------------

        list = [i.start() for i in re.finditer('##', html2)]
        list.reverse()
        for i in list:
            Num4 = i + html2[i:].index('<p>')
            str1 = html2[0:Num4] + '### '
            Num5 = i + html2[i:].index('</p>')
            str2 = html2[(Num4 + 3):Num5].replace('\n', '')
            str3 = html2[Num5:]
            html2 = str1 + str2 + str3  # 二级题目解决完毕

        # ----------------------------------------------------
        html2 = xia.md2html(html2)
        return html2

    @staticmethod
    def email_send(receiver, subject, text):
        username = 'xiamenglei321@163.com'
        password = #此处请写自己的密码
        sender = 'xiamenglei321@163.com'
        subject = Header(subject, 'utf-8').encode()

        msg = MIMEMultipart('mixed')
        msg['Subject'] = subject
        msg['From'] = 'xiamenglei321@163.com <xiamenglei321@163.com>'
        msg['To'] = ";".join(receiver)

        text_plain = MIMEText(text, _subtype='html', _charset='utf-8')
        msg.attach(text_plain)

        smtp = smtplib.SMTP()
        smtp.connect('smtp.163.com')

        smtp.login(username, password)
        smtp.sendmail(sender, receiver, msg.as_string())
        smtp.quit()

    def term_Get(self):
        file = QFileDialog.getOpenFileName(self, '选择文件位置', '.', '*.txt')
        print(file[0])
        with open(file[0], 'r') as f:
            self.QLineEdit1.setText(f.read())

    def html_Get2(self):
        self.html = xia.html_Get(self.QLineEdit1.text())

    def receiver_Get(self):
        file = QFileDialog.getOpenFileName(self, '选择文件位置', '.', '*.txt')
        persions = []
        with open(file[0], 'r') as f:
            for i in f.readlines():
                persions.append(i)
        self.receiver = persions
        self.QLineEdit2.setText(';'.join(persions))
        
    def operation_Run(self):
        self.html_Get2()

        dddd = r'<p>Supplied id parameter is empty.</p>'
        if self.html == dddd:
            reply = QMessageBox.information(self, "状态提醒", "没有论文发表", QMessageBox.Yes | QMessageBox.No)
            return

        text=xia.html_Reshape(self.html)
        subject=self.QLineEdit3.text()
        if subject is None:
            reply=QMessageBox.information(self,"状态提醒","Subject栏不能为空",QMessageBox.Yes|QMessageBox.No)
            return
        xia.email_send(self.receiver, subject, text)
        reply = QMessageBox.information(self, "状态提醒", "已经发送完毕!", QMessageBox.Yes | QMessageBox.No)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = xia()
    win.show()
    sys.exit(app.exec_())
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值