2016-07-24 4 views
0

J'utilise le module feedparser pour créer un flux de nouvelles dans mon programme.Lien (s) de flux RSS séparé (s)

Yahoo! L'élément de lien Finance API a en réalité deux liens: le lien Yahoo, et le lien d'article réel (site/source externe). Les deux sont séparés par un astérisque, à ce qui suit est un exemple:

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

Notez l'astérisque entre les deux éléments. Je me demandais simplement s'il y avait une façon pythonique de séparer ces deux-là, et de ne lire que le second lien dans un fichier.

Nous vous remercions de votre temps.

Voici mon code correspondant:

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) 

Répondre

0

Vous pouvez partager ce de la manière suivante:

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('*') 

Gardez à l'esprit que cela nécessite le lien toujours contenir l'astérisque, sinon vous obtenir l'exception suivante:

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

Si vous avez seulement besoin du second lien, vous pouvez également rite:

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

Ceci indique que vous voulez annuler la première valeur de retour. Une autre alternative est:

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

En ce qui concerne votre code: si vous avez une exception partout après que vous avez ouvert votre fichier de sortie, il ne sera pas fermée correctement. Utilisez le gestionnaire de contexte open (docs) ou le bloc try ... finally (docs) pour vous assurer de fermer votre fichier quoi qu'il arrive.

manager Contexte:

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

gestionnaire d'exception:

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