简体   繁体   English

Python-创建支持用户输入的链接列表

[英]Python - Creating a list of links that support user input

Extracted href attributes with BeautifulSoup4 (also using urllib2, webbrowser)... I would like an output similar to this, that supports user input: 使用BeautifulSoup4(也使用urllib2,webbrowser)提取了href属性...我想要一个类似于此的输出,它支持用户输入:

Any ideas? 有任何想法吗?

(0) - http://www.example1.com
(1) - http://www.example2.com
(2) - http://www.example3.com
(3) - http://www.example4.com
(4) - http://www.example5.com
(5) - http://www.example6.com

>>> 0 (opens http://www.example1.com in browser)
>>> 1 (opens http://www.example2.com in browser)

You could write your links to an html file, and use something like os.popen('firefox file.html') to open the file so that the user might click the links directly, or you could use the raw_input() function to ask the user for some option, and then call firefox (or other browser) with command line arguments, again using os.popen() . 您可以将链接写到html文件,并使用os.popen('firefox file.html')之类的文件打开文件,以便用户可以直接单击链接,也可以使用raw_input()函数询问用户使用某些选项,然后再次使用os.popen()使用命令行参数调用firefox(或其他浏览器os.popen()

This worked for me (an example only): 这对我有用(仅一个示例):

import os

link1 = 'www.google.com'
link2 = 'www.yahoo.com'

s = raw_input("1 = %s\n2 = %s\n\n" % (link1, link2))

if s == '1':
    os.popen("firefox %s" % link1)
elif s == '2':
    os.popen("firefox %s" % link2)

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

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