use*_*632 3 python regex rss screen-scraping
我是Python和编程的新手,所以如果问题非常愚蠢,请原谅.
我一直在逐步学习RSS抓取这个教程,但是当我试图收集相应链接到正在收集的文章的标题时,我得到了Python的"列表索引超出范围"错误.
这是我的代码:
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import re
source = urlopen('http://feeds.huffingtonpost.com/huffingtonpost/raw_feed').read()
title = re.compile('<title>(.*)</title>')
link = re.compile('<link>(.*)</link>')
find_title = re.findall(title, source)
find_link = re.findall(link, source)
literate = []
literate[:] = range(1, 16)
for i in literate:
print find_title[i]
print find_link[i]
Run Code Online (Sandbox Code Playgroud)
这时候我只告诉它来获取冠军,但立即引发时,我想检索标题索引错误执行罚款和其对应的链接.
任何帮助将不胜感激.
您可以使用feedparser模块解析来自给定URL的RSS源:
#!/usr/bin/env python
import feedparser # pip install feedparser
d = feedparser.parse('http://feeds.huffingtonpost.com/huffingtonpost/latestnews')
# .. skipped handling http errors, cacheing ..
for e in d.entries:
print(e.title)
print(e.link)
print(e.description)
print("\n") # 2 newlines
Run Code Online (Sandbox Code Playgroud)
Even Critics Of Safety Net Increasingly Depend On It
http://www.huffingtonpost.com/2012/02/12/safety-net-benefits_n_1271867.html
<p>Ki Gulbranson owns a logo apparel shop, deals in
<!-- ... snip ... -->
Christopher Cain, Atlanta Anti-Gay Attack Suspect, Arrested And
Charged With Aggravated Assault And Robbery
http://www.huffingtonpost.com/2012/02/12/atlanta-anti-gay-suspect-christopher-cain-arrested_n_1271811.html
<p>ATLANTA -- Atlanta police have arrested a suspect
<!-- ... snip ... -->
Run Code Online (Sandbox Code Playgroud)
使用正则表达式解析rss(xml)可能不是一个好主意.