2009-12-27 41 views
0

我正在寫一個簡單的Twitter的例子,讀通過Twitter的搜索RSS提要:如何從Twitter搜索Feed獲取純文本結果?

http://search.twitter.com/search.rss 

這種運作良好,只是在本描述包含HTML如粗體標籤和鏈接標籤,我已經看過了Atom feed via:

http://search.twitter.com/search.atom 

它在說明中也有HTML,是否有包含Tweet的純文本的版本?該文件有點含糊不清,加上RSS格式列表中缺少RSS,即使這可行。有沒有祕密的方法,我只想要原始的非HTML的鳴叫!
這甚至是可能的,如果不是,那麼從字符串中剝離HTML的最佳方式是什麼,因爲我需要的只是原始的推文。

+0

我認爲''每次的entry'標籤title'是你所需要的。 – apod 2009-12-27 14:41:24

+0

我不確定標題是否僅僅是一個截斷的內容 - 但是已經檢查並且這是正確的,請將此添加爲答案,並且我會將其標記爲這樣。 – RoguePlanetoid 2009-12-27 15:23:40

回答

1

我認爲每個entrytitle標籤都是您所需要的。

+0

我沒有注意到標題和描述與我在瀏覽器中查看RSS時相同,並且這是截斷標題,並且在代碼中我看到了HTML,因此它們顯得不同。 – RoguePlanetoid 2009-12-28 11:43:56

1

removeHtmlTags來自this post的模板可以實現您正在查找的內容。

<xsl:template name="removeHtmlTags"> 
    <xsl:param name="html"/> 
    <xsl:choose> 
     <xsl:when test="contains($html, '&lt;')"> 
      <xsl:value-of select="substring-before($html, '&lt;')"/> 
      <!-- Recurse through HTML --> 
      <xsl:call-template name="removeHtmlTags"> 
       <xsl:with-param name="html" select="substring-after($html, '&gt;')"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$html"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

您沒有指定想要的輸出結果。這裏是一個簡單的例子,在文本前面打印每個RSS項目描述元素爲「描述X.」「」。

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output indent="yes" method="text"/> 

    <xsl:template match="/"> 
    <xsl:apply-templates select="rss/channel/item/description"/> 
    </xsl:template> 

<xsl:template match="description"> 
<!--This adds a label for each description: --> 
Description <xsl:value-of select="position()"/>.) 
    <xsl:call-template name="removeHtmlTags"> 
     <xsl:with-param name="html" select="." /> 
    </xsl:call-template> 

</xsl:template> 

<xsl:template name="removeHtmlTags"> 
    <xsl:param name="html"/> 
    <xsl:choose> 
     <xsl:when test="contains($html, '&lt;')"> 
      <xsl:value-of select="substring-before($html, '&lt;')"/> 
      <!-- Recurse through HTML --> 
      <xsl:call-template name="removeHtmlTags"> 
       <xsl:with-param name="html" select="substring-after($html, '&gt;')"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$html"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 

運行鍼對this RSS formatted search該樣式表生成以下的輸出:

Description 1.) 
    Ke$ha desbanca Jay-Z e Lady Gaga na parada norte-americana #adorei 
Description 2.) 
    STR8HIPHOP: VIDEO: Just Blaze Talks Baseline Records Studio &amp; His Work With Jay-Z: 
    LTD Magazine talks to Just ... http://bit.ly/7y19WU 
Description 3.) 
    Nikon D3000 10MP $449.95 http://bit.ly/55gKZZ , DJ Hero Renegade Edition Featuring Jay-Z and Eminem $149 Amazon Now HURRY! http://bit.ly/5oW 
Description 4.) 
    #nowplaying Empire State Of Mind , jay-z and alcia keys ! 
Description 5.) 
    #NowPlaying R. Kelly - Fiesta Remix feat_ Jay-Z http://qstat.us/z6lp 
Description 6.) 
    50 Wants To Know Why Its Wrong For Beanie Sigel To Diss Jay-z But When The Game Disrespects Cu... http://tinyurl.com/yj5fma2 
Description 7.) 
    Nuove Foto: Mariah, Beyonce e Jay-Z, Monica! http://bit.ly/4Em2HL 
Description 8.) 
    NOW PLAYING: Jay-Z - Young Forever [Jay-Z + Mr Hudson] [Album Version] http://www.qmr.fm 
Description 9.) 
    Jay-Z tops News&apos; list of Hot New Yorkers in 2009 - http://shar.es/a91pQ 
Description 10.) 
    VIDEO: Just Blaze Talks Baseline Records Studio &amp; His Work With Jay-Z: 
LTD Magazine talks to Just Blaze about.. http://tinyurl.com/yzx3str 
Description 11.) 
    Download Cookin&apos; Soul Presents: Game &amp; Jay-Z – The RED Album | The …: Mash up posse producers Cookin&apos; Sou.. http://bit.ly/70rDNk 
Description 12.) 
    @Plex_Luthor you and Jay-Z! no way! lol The secret handshake ftw. 
Description 13.) 
    Questlove and Jay-Z make a bet on the World Series (The Fightins) http://o-x.fr/9yr7 
Description 14.) 
    Clip Jay-Z ft. Mr. Hudson - Young Forever: http://www.youtube.com/v/E1nbvplgElw http://bit.ly/4ze0If 
Description 15.) 
    musica nova → Jay-Z ft. Alicia Keys - Empire state of mind (New York) .. mto filé 
+0

這很方便 - 雖然RSS確實返回了一個不含HTML的值作爲標題,但可能會出現這種情況,我可能需要從RSS中去除HTML,所以非常感謝。 – RoguePlanetoid 2009-12-28 11:42:35