Rss Feed与BeautifulSoup刮擦

问题描述:

我遇到了麻烦与我的脚本。我能够获得标题和链接,但我似乎无法打开文章并刮掉文章。有人可以请帮忙!Rss Feed与BeautifulSoup刮擦

from urllib import urlopen 
from BeautifulSoup import BeautifulSoup 
import re 

source = urlopen('http://www.marketingmag.com.au/feed/').read() 

title = re.compile('<title>(.*)</title>') 
link = re.compile('<a href="(.*)">') 

find_title = re.findall(title, source) 
find_link = re.findall(link, source) 



literate = [] 
literate[:] = range(1, 10) 

for i in literate: 
    print find_title[i] 
    print find_link[i] 

articlePage = urlopen(find_link[i]).read() 

divBegin = articlePage.find('<div class="entry-content">') 

article = articlePage[divBegin:(divBegin+1000)] 

soup = BeautifulSoup(article) 

paragList = soup.findAll('p') 

for i in paragList: 
     print i 
     print ("\n") 
+1

尝试在每行代码前面放置4个空格或选择所有代码,然后单击“代码示例”按钮(带有大括号的代码),以使代码更具可读性。如果您可以向我们展示一些当前输出的样本行,并且最好还需要输出,它也会有所帮助:) – 2012-04-02 06:57:42

不要使用正则表达式来解析HTML。只需使用美丽的汤,它的设施如find_all获得链接,然后你可以使用urllib2.urlopen打开网址,然后阅读内容。

代码强烈提醒我:http://www.youtube.com/watch?v=Ap_DlSrT-iE

为什么你实际使用BeautifulSoup XML解析?它为HTML-Sites和python本身构建了非常好的XML解析器。例如:http://docs.python.org/library/xml.dom.minidom.html

http://www.crummy.com/software/BeautifulSoup/bs4/doc/应该告诉你,你应该只是使用beatifulsoup来加载该URL并解析结果结构。祝你好运!