- Python - 文字處理
- Python - 文字處理入門
- Python - 文字處理環境
- Python - 字串不變性
- Python - 排序行
- Python - 段落重新格式化
- Python - 統計段落中的詞元
- Python - 二進位制 ASCII 轉換
- Python - 字串作為檔案
- Python - 反向讀取檔案
- Python - 過濾重複單詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取 URL
- Python - 美化列印
- Python - 文字處理狀態機
- Python - 首字母大寫和翻譯
- Python - 分詞
- Python - 去除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 單詞替換
- Python - 拼寫檢查
- Python - WordNet 介面
- Python - 語料庫訪問
- Python - 詞性標註
- Python - 塊和塊隙
- Python - 塊分類
- Python - 文字分類
- Python - 二元語法
- Python - 處理 PDF
- Python - 處理 Word 文件
- Python - 讀取 RSS Feed
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字處理
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹提取演算法
- Python - 受限搜尋
Python - 讀取 RSS Feed
RSS(網站摘要)是一種用於傳輸定期更改的網路內容的格式。許多新聞相關的網站、網路日誌和其他線上出版商將其內容作為 RSS Feed 傳播給任何想要它的人。在 Python 中,我們藉助以下包來讀取和處理這些 Feed。
pip install feedparser
Feed 結構
在下面的示例中,我們獲取 Feed 的結構,以便我們可以進一步分析我們想要處理的 Feed 的哪些部分。
import feedparser
NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")
entry = NewsFeed.entries[1]
print entry.keys()
執行上述程式後,我們將得到以下輸出:
['summary_detail', 'published_parsed', 'links', 'title', 'summary', 'guidislink', 'title_detail', 'link', 'published', 'id']
Feed 標題和文章
在下面的示例中,我們讀取 RSS Feed 的標題和摘要。
import feedparser
NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")
print 'Number of RSS posts :', len(NewsFeed.entries)
entry = NewsFeed.entries[1]
print 'Post Title :',entry.title
執行上述程式後,我們將得到以下輸出:
Number of RSS posts : 5 Post Title : Cong-JD(S) in SC over choice of pro tem speaker
Feed 詳情
基於上述條目結構,我們可以使用如下所示的 Python 程式從 Feed 中提取必要的詳細資訊。由於條目是一個字典,我們利用它的鍵來產生所需的值。
import feedparser
NewsFeed = feedparser.parse("https://timesofindia.indiatimes.com/rssfeedstopstories.cms")
entry = NewsFeed.entries[1]
print entry.published
print "******"
print entry.summary
print "------News Link--------"
print entry.link
執行上述程式後,我們將得到以下輸出:
Fri, 18 May 2018 20:13:13 GMT ****** Controversy erupted on Friday over the appointment of BJP MLA K G Bopaiah as pro tem speaker for the assembly, with Congress and JD(S) claiming the move went against convention that the post should go to the most senior member of the House. The combine approached the SC to challenge the appointment. Hearing is scheduled for 10:30 am today. ------News Link-------- https://timesofindia.indiatimes.com/india/congress-jds-in-sc-over-bjp-mla-made-pro-tem-speaker-hearing-at-1030-am/articleshow/64228740.cms
廣告