【问题标题】:Separate RSS feed link/s单独的 RSS 提要链接
【发布时间】:2016-11-27 20:10:15
【问题描述】:

我正在使用 feedparser 模块在我的程序中创建新闻提要。

雅虎! Finance API 链接元素实际上有两个链接:Yahoo 链接和实际文章链接(外部站点/来源)。两者用星号隔开,以下为示例:

'http://us.rd.yahoo.com/finance/external/investors/rss/SIG=12shc077a/*http://www.investors.com/news/technology/click/pokemon-go-hurting-facebook-snapchat-usage/'

注意两个项目之间的星号。

我只是想知道是否有pythonic方法可以将这两者分开,并且只读取文件的第二个链接。

感谢您的宝贵时间。

这是我的相关代码:

def parse_feed(news_feed_message, rss_url):
    ''' This function parses the Yahoo! RSS API for data of the latest five articles, and writes it to the company news text file'''

    # Define the RSS feed to parse from, as the url passed in of the company the user chose
    feed = feedparser.parse(rss_url)

    # Define the file to write the news data to the company news text file
    outFile = open('C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Text Files\\companyNews.txt', mode='w')

    # Create a list to store the news data parsed from the Yahoo! RSS
    news_data_write = []
    # Initialise a count
    count = 0
    # For the number of articles to append to the file, append the article's title, link, and published date to the news_elements list
    for count in range(10):
        news_data_write.append(feed['entries'][count].title)
        news_data_write.append(feed['entries'][count].published)
        news_data_write.append(feed['entries'][count].link)
        # Add one to the count, so that the next article is parsed
        count+=1
        # For each item in the news_elements list, convert it to a string and write it to the company news text file
        for item in news_data_write:
            item = str(item)
            outFile.write(item+'\n')
        # For each article, write a new line to the company news text file, so that each article's data is on its own line
        outFile.write('\n')
        # Clear the news_elements list so that data is not written to the file more than once
        del(news_data_write[:])
    outFile.close()

    read_news_file(news_feed_message)

【问题讨论】:

    标签: python python-3.x rss feedparser


    【解决方案1】:

    您可以通过以下方式拆分:

    link = 'http://us.rd.yahoo.com/finance/external/investors/rss/SIG=12shc077a/*http://www.investors.com/news/technology/click/pokemon-go-hurting-facebook-snapchat-usage/'
    
    rss_link, article_link = link.split('*')
    

    请记住,这要求链接始终包含星号,否则您将收到以下异常:

    ValueError: not enough values to unpack (expected 2, got 1)
    

    如果你只需要第二个链接,你也可以写:

    _, article_link = link.split('*')
    

    这表示您要丢弃第一个返回值。 另一种选择是:

    article_link = link.split('*')[1]
    

    关于您的代码:如果您在打开输出文件后的任何地方出现异常,它将无法正确关闭。使用 open 上下文管理器 (docs) 或 try ... finally 块 (docs) 确保无论发生什么都关闭文件。

    上下文管理器:

    with open('youroutputfile', 'w') as f:
        # your code
        f.write(…)
    

    异常处理程序:

    try:
        f = open('youroutputfile', 'w')
        f.write(…)
    finally:
        f.close()
    

    【讨论】:

      猜你喜欢
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      相关资源
      最近更新 更多