2

I have some links in a list and I need to open all these links in my browser

import webbrowser
browserpath='C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'

lis=[ITS FILLED WITH LINK ADDRESSES]

for site in lis:
    webbrowser.get(browserpath).open_new_tab(site)

it just executes and finishing the program but nothing opens

I'm using Python 3.11.1

upd.

It seems it's working fine without webbrowser.get()

Another question is: how would I specify which browser I need them to be opened with? If I need to open it with Firefox I would try:

import webbrowser
lis = ["https://www.google.com", "https://www.youtube.com", "https://www.facebook.com"]
fire="C:\programs\Browser\firefox.exe %s"
for site in lis:
    webbrowser.get(fire).open_new_tab(site)

but it doesn't work too, can you explain it to me please?

Lesbian
  • 21
  • 3

1 Answers1

0

You are using webbrowser.get which returns a controller object for the browser type. If you do this, you should then use that object to open sites. e.g.

import webbrowser

lis = ["https://www.google.com", "https://www.youtube.com", "https://www.facebook.com"]

fire="C:\programs\Browser\firefox.exe"

controller = webbrowser.get(using=fire)

for url in urls:
    controller.open(url, new=2)  # Open in a new tab if possible

(Ensure that your path to your browser executable is correct of course.)

If you just want the default browser suitable for the launch environment, you can use this:

import webbrowser

urls = ["https://stackoverflow.com", "https://docs.python.org/3.10/library/webbrowser.html#module-webbrowser"]

for url in urls:
    webbrowser.open(url, new=2)  # Open in a new tab in same default browser if possible
bigkeefer
  • 576
  • 1
  • 6
  • 13