{"id":112934,"date":"2018-08-03T08:24:39","date_gmt":"2018-08-03T00:24:39","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=112934"},"modified":"2018-07-27T09:19:36","modified_gmt":"2018-07-27T01:19:36","slug":"python-rss","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-rss.html","title":{"rendered":"\u7528 Python \u53bb\u6784\u5efa\u4e00\u4e2a RSS \u63d0\u793a\u7cfb\u7edf"},"content":{"rendered":"
\u4eba\u751f\u82e6\u77ed\uff0c\u6211\u7528 Python\uff0cPython \u662f\u975e\u5e38\u68d2\u7684\u5feb\u901f\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 Python \u53bb\u6784\u5efa\u4e00\u4e2a RSS \u63d0\u793a\u7cfb\u7edf\uff0c\u76ee\u6807\u662f\u4f7f\u7528 Fedora \u5feb\u4e50\u5730\u5b66\u4e60 Python\u3002\u5982\u679c\u4f60\u6b63\u5728\u5bfb\u627e\u4e00\u4e2a\u5b8c\u6574\u7684 RSS \u63d0\u793a\u5e94\u7528\u7a0b\u5e8f\uff0c\u5728 Fedora \u4e2d\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u51e0\u4e2a\u5305\u3002<\/p>\n
Python 3.6 \u5728 Fedora \u4e2d\u662f\u9ed8\u8ba4\u5b89\u88c5\u7684\uff0c\u5b83\u5305\u542b\u4e86 Python \u7684\u5f88\u591a\u6807\u51c6\u5e93\u3002\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u53ef\u4ee5\u8ba9\u6211\u4eec\u7684\u4efb\u52a1\u66f4\u52a0\u7b80\u5355\u5b8c\u6210\u7684\u6a21\u5757\u7684\u96c6\u5408\u3002\u4f8b\u5982\uff0c\u5728\u6211\u4eec\u7684\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 sqlite3\u6a21\u5757\u5728\u6570\u636e\u5e93\u4e2d\u53bb\u521b\u5efa\u8868\u3001\u6dfb\u52a0\u548c\u8bfb\u53d6\u6570\u636e\u3002\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bd5\u56fe\u53bb\u89e3\u51b3\u7684\u662f\u8fd9\u6837\u7684\u4e00\u4e2a\u7279\u5b9a\u95ee\u9898\uff0c\u5728\u6807\u51c6\u5e93\u4e2d\u6ca1\u6709\u5305\u542b\uff0c\u800c\u6709\u53ef\u80fd\u5df2\u7ecf\u6709\u4eba\u4e3a\u6211\u4eec\u5f00\u53d1\u4e86\u8fd9\u6837\u4e00\u4e2a\u6a21\u5757\u3002\u6700\u597d\u662f\u4f7f\u7528\u50cf\u5927\u5bb6\u719f\u77e5\u7684 PyPI Python \u5305\u7d22\u5f15\u53bb\u641c\u7d22\u4e00\u4e0b\u3002\u5728\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 feedparser\u53bb\u89e3\u6790 RSS \u6e90\u3002
\n\u56e0\u4e3a feedparser \u5e76\u4e0d\u662f\u6807\u51c6\u5e93\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b83\u5b89\u88c5\u5230\u6211\u4eec\u7684\u7cfb\u7edf\u4e0a\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u5728 Fedora \u4e2d\u6709\u8fd9\u4e2a RPM \u5305\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u884c\u5982\u4e0b\u7684\u547d\u4ee4\u53bb\u5b89\u88c5 feedparser\uff1a<\/p>\n
$ sudo dnf install python3-feedparser<\/pre>\n\u6211\u4eec\u73b0\u5728\u5df2\u7ecf\u62e5\u6709\u4e86\u7f16\u5199\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u6240\u9700\u7684\u4e1c\u897f\u4e86\u3002<\/p>\n
\u5b58\u50a8\u6e90\u6570\u636e<\/strong><\/div>\n\u6211\u4eec\u9700\u8981\u5b58\u50a8\u5df2\u7ecf\u53d1\u5e03\u7684\u6587\u7ae0\u7684\u6570\u636e\uff0c\u8fd9\u6837\u6211\u4eec\u7684\u7cfb\u7edf\u5c31\u53ef\u4ee5\u53ea\u63d0\u793a\u65b0\u53d1\u5e03\u7684\u6587\u7ae0\u3002\u6211\u4eec\u8981\u4fdd\u5b58\u7684\u6570\u636e\u5c06\u662f\u7528\u6765\u8fa8\u522b\u4e00\u7bc7\u6587\u7ae0\u7684\u552f\u4e00\u65b9\u6cd5\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u5b58\u50a8\u6587\u7ae0\u7684\u6807\u9898\u548c\u53d1\u5e03\u65e5\u671f\u3002
\n\u56e0\u6b64\uff0c\u6211\u4eec\u6765\u4f7f\u7528 Python sqlite3 \u6a21\u5757\u548c\u4e00\u4e2a\u7b80\u5355\u7684 SQL \u8bed\u53e5\u6765\u521b\u5efa\u6211\u4eec\u7684\u6570\u636e\u5e93\u3002\u540c\u65f6\u4e5f\u6dfb\u52a0\u4e00\u4e9b\u540e\u9762\u5c06\u8981\u7528\u5230\u7684\u6a21\u5757\uff08feedparse\uff0csmtplib\uff0c\u548c email\uff09\u3002<\/p>\n\u521b\u5efa\u6570\u636e\u5e93<\/strong><\/div>\n#!\/usr\/bin\/python3\r\nimport sqlite3\r\nimport smtplib\r\nfrom email.mime.text import MIMEText\r\nimport feedparser\r\ndb_connection = sqlite3.connect('\/var\/tmp\/magazine_rss.sqlite')\r\ndb = db_connection.cursor()\r\ndb.execute(' CREATE TABLE IF NOT EXISTS magazine (title TEXT, date TEXT)')<\/pre>\n\u8fd9\u51e0\u884c\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a magazine_rss.sqlite \u6587\u4ef6\u7684\u65b0 sqlite \u6570\u636e\u5e93\uff0c\u7136\u540e\u5728\u6570\u636e\u5e93\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a magazine \u7684\u65b0\u8868\u3002\u8fd9\u4e2a\u8868\u6709\u4e24\u4e2a\u5217 \u2014\u2014 title \u548c date \u2014\u2014 \u5b83\u4eec\u80fd\u5b58\u8bf8 TEXT \u7c7b\u578b\u7684\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u8bf4\u6bcf\u4e2a\u5217\u7684\u503c\u90fd\u662f\u6587\u672c\u5b57\u7b26\u3002<\/p>\n
\u68c0\u67e5\u6570\u636e\u5e93\u4e2d\u7684\u65e7\u6587\u7ae0<\/strong><\/div>\n\u7531\u4e8e\u6211\u4eec\u4ec5\u5e0c\u671b\u589e\u52a0\u65b0\u7684\u6587\u7ae0\u5230\u6211\u4eec\u7684\u6570\u636e\u5e93\u4e2d\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u529f\u80fd\u53bb\u68c0\u67e5 RSS \u6e90\u4e2d\u7684\u6587\u7ae0\u5728\u6570\u636e\u5e93\u4e2d\u662f\u5426\u5b58\u5728\u3002\u6211\u4eec\u5c06\u6839\u636e\u5b83\u6765\u5224\u65ad\u662f\u5426\u53d1\u9001\uff08\u6709\u65b0\u6587\u7ae0\u7684\uff09\u90ae\u4ef6\u63d0\u793a\u3002Ok\uff0c\u73b0\u5728\u6211\u4eec\u6765\u5199\u8fd9\u4e2a\u529f\u80fd\u7684\u4ee3\u7801\u3002<\/p>\n
def article_is_not_db(article_title, article_date):\r\n\"\"\" Check if a given pair of article title and date\r\nis in the database.\r\nArgs:\r\narticle_title (str): The title of an article\r\narticle_date (str): The publication date of an article\r\nReturn:\r\nTrue if the article is not in the database\r\nFalse if the article is already present in the database\r\n\"\"\"\r\ndb.execute(\"SELECT * from magazine WHERE title=? AND date=?\", (article_title, article_date))\r\nif not db.fetchall():\r\nreturn True\r\nelse:\r\nreturn False<\/pre>\n\u8fd9\u4e2a\u529f\u80fd\u7684\u4e3b\u8981\u90e8\u5206\u662f\u4e00\u4e2a SQL \u67e5\u8be2\uff0c\u6211\u4eec\u8fd0\u884c\u5b83\u53bb\u641c\u7d22\u6570\u636e\u5e93\u3002\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a SELECT \u547d\u4ee4\u53bb\u5b9a\u4e49\u6211\u4eec\u5c06\u8981\u5728\u54ea\u4e2a\u5217\u4e0a\u8fd0\u884c\u8fd9\u4e2a\u67e5\u8be2\u3002\u6211\u4eec\u4f7f\u7528 * \u7b26\u53f7\u53bb\u9009\u53d6\u6240\u6709\u5217\uff08title \u548c date\uff09\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u67e5\u8be2\u7684 WHERE \u6761\u4ef6 article_title \u548c article_date \u53bb\u5339\u914d\u6807\u9898\u548c\u65e5\u671f\u5217\u4e2d\u7684\u503c\uff0c\u4ee5\u68c0\u7d22\u51fa\u6211\u4eec\u9700\u8981\u7684\u5185\u5bb9\u3002
\n\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u8fd4\u56de True \u6216\u8005 False \u7684\u903b\u8f91\u6765\u8868\u793a\u662f\u5426\u5728\u6570\u636e\u5e93\u4e2d\u627e\u5230\u5339\u914d\u7684\u6587\u7ae0\u3002<\/p>\n\u5728\u6570\u636e\u5e93\u4e2d\u6dfb\u52a0\u65b0\u6587\u7ae0<\/strong><\/div>\n\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5199\u4e00\u4e9b\u4ee3\u7801\u53bb\u6dfb\u52a0\u65b0\u6587\u7ae0\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n
def add_article_to_db(article_title, article_date):\r\n\"\"\" Add a new article title and date to the database\r\nArgs:\r\narticle_title (str): The title of an article\r\narticle_date (str): The publication date of an article\r\n\"\"\"\r\ndb.execute(\"INSERT INTO magazine VALUES (?,?)\", (article_title, article_date))\r\ndb_connection.commit()<\/pre>\n\u8fd9\u4e2a\u529f\u80fd\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a SQL \u67e5\u8be2\u53bb\u63d2\u5165\u4e00\u4e2a\u65b0\u884c\u5230 magazine \u8868\u7684 article_title \u548c article_date \u5217\u4e2d\u3002\u7136\u540e\u63d0\u4ea4\u5b83\u5230\u6570\u636e\u5e93\u4e2d\u6c38\u4e45\u4fdd\u5b58\u3002
\n\u8fd9\u4e9b\u5c31\u662f\u5728\u6570\u636e\u5e93\u4e2d\u6240\u9700\u8981\u7684\u4e1c\u897f\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e0b\uff0c\u5982\u4f55\u4f7f\u7528 Python \u5b9e\u73b0\u63d0\u793a\u7cfb\u7edf\u548c\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u3002<\/p>\n\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u63d0\u793a<\/strong><\/div>\n\u6211\u4eec\u4f7f\u7528 Python \u6807\u51c6\u5e93\u6a21\u5757 smtplib \u6765\u521b\u5efa\u4e00\u4e2a\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u7684\u529f\u80fd\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684 email \u6a21\u5757\u53bb\u683c\u5f0f\u5316\u6211\u4eec\u7684\u7535\u5b50\u90ae\u4ef6\u4fe1\u606f\u3002<\/p>\n
def send_notification(article_title, article_url):\r\n\"\"\" Add a new article title and date to the database\r\n\r\nArgs:\r\narticle_title (str): The title of an article\r\narticle_url (str): The url to access the article\r\n\"\"\"\r\n\r\nsmtp_server = smtplib.SMTP('smtp.gmail.com', 587)\r\nsmtp_server.ehlo()\r\nsmtp_server.starttls()\r\nsmtp_server.login('your_email@gmail.com', '123your_password')\r\nmsg = MIMEText(f'\\nHi there is a new Fedora Magazine article : {article_title}. \\nYou can read it here {article_url}')\r\nmsg['Subject'] = 'New Fedora Magazine Article Available'\r\nmsg['From'] = 'your_email@gmail.com'\r\nmsg['To'] = 'destination_email@gmail.com'\r\nsmtp_server.send_message(msg)\r\nsmtp_server.quit()<\/pre>\n\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4f7f\u7528\u4e86\u8c37\u6b4c\u90ae\u4ef6\u7cfb\u7edf\u7684 smtp \u670d\u52a1\u5668\u53bb\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff0c\u5728\u4f60\u81ea\u5df1\u7684\u4ee3\u7801\u4e2d\u4f60\u9700\u8981\u5c06\u5b83\u66f4\u6539\u4e3a\u4f60\u81ea\u5df1\u7684\u7535\u5b50\u90ae\u4ef6\u670d\u52a1\u63d0\u4f9b\u8005\u7684 SMTP \u670d\u52a1\u5668\u3002\u8fd9\u4e2a\u529f\u80fd\u662f\u4e2a\u6837\u677f\uff0c\u5927\u591a\u6570\u7684\u5185\u5bb9\u8981\u6839\u636e\u4f60\u7684 smtp \u670d\u52a1\u5668\u7684\u53c2\u6570\u6765\u914d\u7f6e\u3002\u4ee3\u7801\u4e2d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u548c\u51ed\u8bc1\u4e5f\u8981\u66f4\u6539\u4e3a\u4f60\u81ea\u5df1\u7684\u3002
\n\u5982\u679c\u5728\u4f60\u7684 Gmail \u5e10\u6237\u4e2d\u4f7f\u7528\u4e86\u53cc\u56e0\u5b50\u8ba4\u8bc1\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u914d\u7f6e\u4e00\u4e2a\u5bc6\u7801\u5e94\u7528\u7a0b\u5e8f\u4e3a\u4f60\u7684\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e00\u4e2a\u552f\u4e00\u5bc6\u7801\u3002\u53ef\u4ee5\u770b\u8fd9\u4e2a \u5e2e\u52a9\u9875\u9762\u3002<\/p>\n\u8bfb\u53d6 Fedora Magazine \u7684 RSS \u6e90<\/strong><\/div>\n\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u6587\u7ae0\u548c\u53d1\u9001\u63d0\u793a\u7535\u5b50\u90ae\u4ef6\u7684\u529f\u80fd\uff0c\u73b0\u5728\u6765\u521b\u5efa\u4e00\u4e2a\u89e3\u6790 Fedora Magazine RSS \u6e90\u5e76\u63d0\u53d6\u6587\u7ae0\u6570\u636e\u7684\u529f\u80fd\u3002<\/p>\n
def read_article_feed():\r\n\"\"\" Get articles from RSS feed \"\"\"\r\nfeed = feedparser.parse('https:\/\/fedoramagazine.org\/feed\/')\r\nfor article in feed['entries']:\r\nif article_is_not_db(article['title'], article['published']):\r\nsend_notification(article['title'], article['link'])\r\nadd_article_to_db(article['title'], article['published'])\r\n\r\nif __name__ == '__main__':\r\nread_article_feed()\r\ndb_connection.close()<\/pre>\n\u5728\u8fd9\u91cc\u6211\u4eec\u5c06\u4f7f\u7528 feedparser.parse \u529f\u80fd\u3002\u8fd9\u4e2a\u529f\u80fd\u8fd4\u56de\u4e00\u4e2a\u7528\u5b57\u5178\u8868\u793a\u7684 RSS \u6e90\uff0c\u5bf9\u4e8e feedparser \u7684\u5b8c\u6574\u63cf\u8ff0\u53ef\u4ee5\u53c2\u8003\u5b83\u7684 \u6587\u6863\u3002
\nRSS \u6e90\u89e3\u6790\u5c06\u8fd4\u56de\u6700\u540e\u7684 10 \u7bc7\u6587\u7ae0\u4f5c\u4e3a entries\uff0c\u7136\u540e\u6211\u4eec\u63d0\u53d6\u4ee5\u4e0b\u4fe1\u606f\uff1a\u6807\u9898\u3001\u94fe\u63a5\u3001\u6587\u7ae0\u53d1\u5e03\u65e5\u671f\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u524d\u9762\u5b9a\u4e49\u7684\u68c0\u67e5\u6587\u7ae0\u662f\u5426\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u7684\u529f\u80fd\uff0c\u7136\u540e\uff0c\u53d1\u9001\u63d0\u793a\u7535\u5b50\u90ae\u4ef6\u5e76\u5c06\u8fd9\u4e2a\u6587\u7ae0\u6dfb\u52a0\u5230\u6570\u636e\u5e93\u4e2d\u3002
\n\u5f53\u8fd0\u884c\u6211\u4eec\u7684\u811a\u672c\u65f6\uff0c\u6700\u540e\u7684 if \u8bed\u53e5\u8fd0\u884c\u6211\u4eec\u7684 read_article_feed \u529f\u80fd\uff0c\u7136\u540e\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n\u8fd0\u884c\u6211\u4eec\u7684\u811a\u672c<\/strong><\/div>\n\u7ed9\u811a\u672c\u6587\u4ef6\u8d4b\u4e8e\u6b63\u786e\u8fd0\u884c\u6743\u9650\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528 cron \u5b9e\u7528\u7a0b\u5e8f\u53bb\u6bcf\u5c0f\u65f6\u81ea\u52a8\u8fd0\u884c\u4e00\u6b21\u6211\u4eec\u7684\u811a\u672c\u3002cron \u662f\u4e00\u4e2a\u4f5c\u4e1a\u8ba1\u5212\u7a0b\u5e8f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u5728\u4e00\u4e2a\u56fa\u5b9a\u7684\u65f6\u95f4\u53bb\u8fd0\u884c\u4e00\u4e2a\u4efb\u52a1\u3002<\/p>\n
$ chmod a+x my_rss_notifier.py\r\n$ sudo cp my_rss_notifier.py \/etc\/cron.hourly<\/pre>\n\u4e3a\u4e86\u4f7f\u8be5\u6559\u7a0b\u4fdd\u6301\u7b80\u5355\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 cron.hourly \u76ee\u5f55\u6bcf\u5c0f\u65f6\u8fd0\u884c\u4e00\u6b21\u6211\u4eec\u7684\u811a\u672c\uff0c\u5982\u679c\u4f60\u60f3\u5b66\u4e60\u5173\u4e8e cron \u7684\u66f4\u591a\u77e5\u8bc6\u4ee5\u53ca\u5982\u4f55\u914d\u7f6e crontab\uff0c\u8bf7\u9605\u8bfb cron \u7684 wikipedia \u9875\u9762\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.linux-ren.org\/portal.php?mod=view&aid=2486<\/a><\/p>\n