Python Forum
Having my output links become clickable
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having my output links become clickable
#1
I scraped a site and got a list of links.
I  can call the list object to output the links, but they are just in text format in my shell. They are not clickable. 
I'm using Python 3.6 IDLE and my script outputs in the shell. 

What are some ways to make the links interactive, so I can click on the link and go to the link (opening the browser or any other way)?

Do I need to look into another module to do this? 

Thanks. 



my code here
Reply
#2
I know in linux if you output the link in full (ie https and all) https://python-forum.io it becomes clickable in the terminal. Not sure of windows if the same occurs.

Since you called it shell i am assuming your OS is windows??? IF windows command line doesnt do it (assuming there is not trick) you could try Cygwin or cmder as they are linux feel and most likely make a link hypertext
Recommended Tutorials:
Reply
#3
A Pen 
So a list i html.
One simple way is make your links like that,
and use webbrowser module to show links it browser.
import webbrowser

webbrowser.open('file:///C:/1/html/list.html')
Open a local file on disk,
just open the made .html file browser,so will you get path eg file:///something.
Reply
#4
(May-20-2017, 12:28 AM)metulburr Wrote: I know in linux if you output the link in full (ie https and all) https://python-forum.io it becomes clickable in the terminal. Not sure of windows if the same occurs.

Since you called it shell i am assuming your OS is windows??? IF windows command line doesnt do it (assuming there is not trick) you could try Cygwin or cmder as they are linux feel and most likely make a link hypertext

When I meant shell, I meant the Python shell. I guess Python shell is Windows only?  I see, I will see if I can import some linux features. Thank you.

(May-20-2017, 01:05 AM)snippsat Wrote: A Pen 
So a list i html.
One simple way is make your links like that,
and use webbrowser module to show links it browser.
import webbrowser

webbrowser.open('file:///C:/1/html/list.html')
Open a local file on disk,
just open the made .html file browser,so will you get path eg file:///something.




Hi, I'm a little confused as to your instructions. 
I have a list. Add the html tags and then save the list? 
Save it as an html file and then use webbrowser to open the list?

Thanks.
Reply
#5
(May-20-2017, 02:04 AM)bigmit37 Wrote: Add the html tags and then save the list?
Save it as an html file and then use webbrowser to open the list?

If you just have a list of links, the html is simple enough that you don't really need a module to generate it. Make it a .html file, and then use sys.open() and it will automatically open in your default browser.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
My list is just like in the Pen.
I save it as list.html
<ul>
 <li><a href="https://python-forum.io">The best Python forum</a></li>
 <li><a href=https://google.com>Google</a></li>  
</ul>
Then open list.html with webbrowser module as shown.
Or similar as @ichabod801 suggests,the result will be the same.
import os

os.startfile("list.html")
Quote:I have a list.
Have you scraped it as html links?
<a href="https://python-forum.io">The best Python forum</a>
It should be no problem to make this.
Reply
#7
(May-20-2017, 03:02 AM)snippsat Wrote: My list is just like in the Pen.
I save it as list.html
<ul>
 <li><a href="https://python-forum.io">The best Python forum</a></li>
 <li><a href=https://google.com>Google</a></li>  
</ul>
Then open list.html with webbrowser module as shown.
Or similar as @ichabod801 suggests,the result will be the same.
import os

os.startfile("list.html")
Quote:I have a list.
Have you scraped it as html links?
<a href="https://python-forum.io">The best Python forum</a>
It should be no problem to make this.


I understand! Got it working.
Only problem is I'm having trouble adding an empty line in between each link, despite adding \n in my code:

links =(soup.find_all('a', class_ ='link'))
   with open ('Marketwatch.html', 'w') as file:
       for i in links:
        file.write(str(i)+ '\n')


   webbrowser.open('Marketwatch.html')
Here is how my output looks
[Image: rm1gU9h]
Reply
#8
You could replace \n with <br>,
but better making it a html list bye add <li>.

lst.txt:
Output:
<a href="https://python-forum.io">The best Python forum</a> <a href="https://python-forum.io">The best Python forum</a>
with open('lst.txt') as f,open('link.html', 'w') as f_out:
   for link in f:
       link = link.strip()
       make_list = '<li>{}</li>'.format(link)
       f_out.write('{}\n'.format(make_list))
link.html:
Output:
<li><a href="https://python-forum.io">The best Python forum</a></li> <li><a href="https://python-forum.io">The best Python forum</a></li>
Reply
#9
(May-25-2017, 08:05 PM)snippsat Wrote: You could replace \n with <br>,
but better making it a html list bye add <li>.

lst.txt:
Output:
<a href="https://python-forum.io">The best Python forum</a> <a href="https://python-forum.io">The best Python forum</a>
with open('lst.txt') as f,open('link.html', 'w') as f_out:
   for link in f:
       link = link.strip()
       make_list = '<li>{}</li>'.format(link)
       f_out.write('{}\n'.format(make_list))
link.html:
Output:
<li><a href="https://python-forum.io">The best Python forum</a></li> <li><a href="https://python-forum.io">The best Python forum</a></li>



This works great. (I tried the list feature of html). Thank you.   Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad "Disabled" Buttons Still Clickable. Nd4SpdSe 3 325 Mar-13-2024, 07:44 AM
Last Post: laughorchestra
  help with url links- href links don't work properly DeBug_0neZer0 1 1,977 Jan-06-2021, 11:01 PM
Last Post: DeBug_0neZer0
  How to turn screen output into clickable hyperlinks windros 5 2,720 Jan-22-2019, 05:41 PM
Last Post: windros

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020