简体   繁体   English

如何将值从输出传递到python中的字符串

[英]how to pass value from output to a string in python

I've been trying to make an application in python and I'm new to python. 我一直在尝试在python中创建应用程序,而我是python的新手。 Well, what I actually want to do is that . 好吧,我实际上想做的是。 I want the feedparser to read the values from an RSS of a website... say reddit... and then I want to make that output as a stringand pass the value further to my code... my code right now.. 我希望feedparser从网站的RSS读取值...说reddit ...,然后我要将该输出作为字符串并将值进一步传递给我的代码...我的代码。

import feedparser
import webbrowser

feed = feedparser.parse('http://www.reddit.com/.rss')
print feed['entries'][1]['title'] 
print feed['entries'][1]['link'] 

It is working right now.. it parses the feed and I get the output I want... Now, I want to use the "link" from the "print feed['entries'][1]['link'] " and use it in the code further... how can I do so..? 它现在正在工作..它解析提要,我得到了想要的输出...现在,我想使用“打印提要['entries'] [1] ['link']”中的“链接”并在代码中进一步使用它。。。 To be more specific.. I want to open that URL in my browser... I concluded to something like this.. 更具体地说..我想在浏览器中打开该URL ...我得出这样的结论..

import feedparser
import webbrowser

feed = feedparser.parse('http://www.reddit.com/.rss')
print feed['entries'][1]['title'] 
print feed['entries'][1]['link'] 

mystring = 'feed['entries'][1]['link']'
webbrowser.open('mystring')

It is of course not working... Please Help... if you need to know anything else.. please let me know... 这当然是行不通的...请帮助...如果您需要了解其他任何信息..请让我知道...

This is Reddit specific so it won't work on other RSS feeds but I thought this might help you. 这是Reddit特有的,因此无法在其他RSS feed上使用,但我认为这可能对您有所帮助。

from __future__ import print_function
import praw


r = praw.Reddit("my_cool_user_agent")
submissions = r.get_front_page()
for x in submissions:
    print("Title: {0} URL: {1} Permalink: {2}".format(x, x.url, x.permalink))
    print ("------------------------------------------------------------")

For Reddit there are 2 URLs that you might be interested in: the actual link that is submitted (the 'external' link... think imgur, etc) and the permalink to the Reddit post itself. 对于Reddit,您可能会感兴趣2个URL:提交的实际链接(“外部”链接...例如imgur等)以及指向Reddit帖子本身的永久链接。

Instead of passing the feed[entries][1][link] as a string, just pass the value inside to the webbrowser. 无需将feed[entries][1][link]作为字符串传递,只需将内部值传递给Web浏览器即可。

Example - 范例-

webbrowser.open(feed['entries'][1]['link'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM