2014-10-09 3 views
0

정규 표현식을 사용하고 특히 re 모듈을 사용하여 RSS 피드의 제목, 날짜 및 내용을 추출합니다. 지금까지 나는 다음과 같은 코드를 사용했습니다 :Python Regex를 사용하여 RSS 피드에서 콘텐츠 추출

Headline: Tim Bresnan denies involvement in Kevin Pietersen parody Twitter account 

Description: I 100% did NOT have any password, and wasnt involved<br /> ECB confirms Alec Stewart reported incident in 2012 <br /><a href="http://www.theguardian.com/sport/2014/oct/08/kevin-pietersen-parody-twitter-account-author-denies-england-players-involved" title=""> Twitter account author denies players were involved</a><br /><a href="http://www.theguardian.com/sport/blog/2014/oct/08/ecb-england-cricket-kevin-pietersen-tom-harrison" title=""> Owen Gibson: ECB at crossroads amid fallout</a><p>Tim Bresnan has denied having any involvement in the controversial @KPgenius Twitter account after Kevin Pietersens autobiography claimed his former England team-mates were behind it.</p><p>In his book, Pietersen revealed the extent to which the account had angered and upset him, and claimed that the accounts author had told the former England wicketkeeper Alec Stewart that some of the guys in the dressing room are tweeting from it.</p><p>Disappointed to be implicated in the <a href="https://twitter.com/hashtag/kpgenius?src=hash">#kpgenius</a> account. I 100% did NOT have any password. And wasn't involved In any posting.</p> <a href="http://www.theguardian.com/sport/2014/oct/09/tim-bresnan-kevin-pietersen-parody-twitter">Continue reading...</a>   

Date: Thu, 09 Oct 2014 11:56:43 GMT 

이러한 결과는 각 뉴스 기사 인쇄됩니다

titles = re.findall(r'<title>(.*?)</title>',html_code) 
    descriptions = re.findall(r'<description>(.*?)</description>',html_code) 
    dates = re.findall(r'<pubDate>(.*?)</pubDate>',html_code) 

    for title in titles: 
     if 'The Guardian' in title: 
      pass 
     else: 
      print "Headline:" ,title 
      print 


    for description in descriptions: 
     if 'Latest news and features from theguardian.com' in description: 
      pass 
     else: 
      print "Description:" ,description 
      print 

    for date in dates: 
     print "Date:" ,date 
     print 

이 코드는 다음과 같은 출력을 제공합니다. 내 질문은 콘텐츠 섹션을 정리하고 모든 HTML 스팸을 제거하는 방법은 무엇입니까? 모든 태그가없는 기사의 기본 정보가 필요합니다. 정규 표현식을 사용하여이를 어떻게 제거 할 수 있습니까 (예 : "& lt;/p & gt;")? 감사합니다

+0

아마도 XML 파서 (xml.etree.ElementTree 또는 lxml)를 사용하는 것이 좋습니다. – mhawke

+0

지금 막 다른 모듈없이 정규 표현식을 사용하려고합니다. – user2747367

답변

-1

str.replace()을 사용하면 특수 HTML 문자를 바꿀 필요가있는 것으로 바꿀 수 있습니다.

관련 문제