2014-07-14 2 views
0

이 코드는 bbc의 뉴스 콘텐츠를 긁어 모으기 위해 작성되었습니다. 지금까지는 작동하지만 단락 태그를 표시합니다. 정규식을 사용하여 html 태그를 제거하려고했지만 여전히 작동하지 않습니다. 제발 이걸로 도움이 필요해.단락 태그를 벗을 수 없습니다.

감사

import feedparser 
from bs4 import BeautifulSoup 
import urllib2 
from urllib2 import urlopen 
import re 
import cookielib 
from cookielib import CookieJar 
import time 
import os 

cj = CookieJar() 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
opener.addheaders= [('User-agent','Mozilla')] 

bbcRSSFeed = feedparser.parse('http://feeds.bbci.co.uk/news/rss.xml') 

numberstories=[len(bbcRSSFeed)] 
FeedLinks=[] 
FeedTitles=[] 

for post in bbcRSSFeed.entries: 
    FeedLinks.append(post.link) 
    FeedTitles.append(post.title) 

limit=2 
counter=0 
paraStringList = [] 

for i in FeedLinks: 
    #if counter<FeedLinks: #displays the content of every link 
    if counter<limit: 
     print "["+i +"]" 
     newpage = urlopen(i) 
     soup = BeautifulSoup(newpage) 
     text = soup.select('.story-body p') #content of the news story 
     print (text) 
     counter+=1 

답변

2

선택한 요소에서 불과 텍스트를 원하는 경우, element.get_text() method을 사용

text = '\n\n'.join([para.get_text(' ', strip=True) for para in soup.select('.story-body p')]) 
+0

3.2.1 ... 감사 – hepzibah

+0

피드, 나는 각 피드에서 콘텐츠를 긁어 할 수 그러나 지금 파일로 자신의 타이틀 개별 TXT 파일에 각 출력을 저장하려고했다 이름 타일 예 : newstitle1.txt, newstitle2.txt ...하지만 지금까지, 아래 코드를 사용하여, 나에게 index.txt 예를 들어 0.txt, 1.txt ....이 문제를 해결하는 방법에 대한 아이디어? handle ('textfile.txt', 'w')을 핸들로 사용 : handle.write ('text') – hepzibah

+0

후속 질문은 의견에 맞지 않습니다. 그것들에 대해 더 나은 새로운 질문을하십시오. –

1
text = "\n".join([s.text for s in soup.select('.story-body p')]) 
0
for x in text.contents: 
    print(x) 

그것이 <p>에서 모든 줄 - 내부 태그.

BeautifulSoup로 RSS에서 매우 도움이되었다

관련 문제