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

本博客结构

    • 利用Python获取最新的sci论文摘要信息并群发邮箱
      • 1. 需要的包:biopython、markdown
      • 2. 正式代码

利用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 = '[email protected]'
        # 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('\n', 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('

') str1 = html2[0:Num4] + '### ' Num5 = i + html2[i:].index('

'
) 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 = '[email protected]' password = #此处请写自己的密码 sender = '[email protected]' subject = Header(subject, 'utf-8').encode() msg = MIMEMultipart('mixed') msg['Subject'] = subject msg['From'] = '[email protected] ' 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'

Supplied id parameter is empty.

'
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_())

你可能感兴趣的:(Python编程,科研研究)