0

I have a .csv file that contains a column of urls (40-50 urls), I want to read the csv file and open all those urls on chrome? Is there a way to accomplish this in python? I'm using the following piece of code to read the csv file.

exampleFile = open('MyFile.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)
final = []
for item in exampleData:
    final.append(item[0])
for item in final:
    ???
Blabber
  • 349
  • 4
  • 19
  • Do you require to see the webs? – EndermanAPM Sep 06 '16 at 08:59
  • 1
    Use the built-in webbrowser module – Padraic Cunningham Sep 06 '16 at 09:05
  • Do you need to open the URLs simultaneously or consecutively (i.e. load first, check it's OK, load next)? And are you planning to do anything with the webpage once it's loaded? Note that if you are planning to just load the pages, similar to a bookmarking application, you can call an external application using the subprocess module (subprocess.call(["chrome.exe", URL]) ). – Alan Sep 06 '16 at 09:13
  • @EndermanAPM, Yes I do! – Blabber Sep 06 '16 at 09:22
  • @Alan, consecutively. – Blabber Sep 06 '16 at 09:24
  • 1
    @Blabber Then, as suggested by all the people Selenium should do the trick if you just need to open them and you don't need to interact with them subprocess will work just fine :) – EndermanAPM Sep 06 '16 at 09:26
  • @Blabber p.s. As stated [here](http://stackoverflow.com/a/10965965/4725649) you can use Popen in order to call chrome without waiting the last one to complete. – EndermanAPM Sep 06 '16 at 09:27

5 Answers5

2

You can use selenium web driver to load each URL in chrome.

Reading the csv file can be improved like this:

from selenium import webdriver

driver = webdriver.Chrome()

with open('MyFile.csv') as example_file:
    example_reader = csv.reader(example_file)
    for row in example_reader:
        driver.get(row[0])
        # do whatever...

    driver.close()
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home – Blabber Sep 06 '16 at 09:53
1

Assuming your posted snippet is alright and final contains valid urls you could do something like this:

import webbrowser

exampleFile = open('MyFile.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)
final = []

for item in exampleData:
    final.append(item[0])
for url in final:
    webbrowser.open_new_tab(url)

For more information take a look to the Convenient Web-browser controller

BPL
  • 9,632
  • 9
  • 59
  • 117
1

Used this in the end to make it work the way I wanted to. Plus I did not have to install any external modules! Thanks a lot for all your answers, they helped me build the final one!

import webbrowser
import csv

path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
exampleFile = open('MyFile.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)

for item in exampleData:
    webbrowser.get(path).open(item[0])
Blabber
  • 349
  • 4
  • 19
0

You can use selenium with Chrome web driver https://sites.google.com/a/chromium.org/chromedriver/getting-started

phi
  • 10,572
  • 3
  • 21
  • 30
0

You can use selenium. First install selenium by pip install selenium. The following code open http://www.python.org in mozilla firefox.You can change driver to chrome driver in selenium to open links in chrome. For chrome you can see How to run Selenium WebDriver test cases in Chrome?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.python.org")
Community
  • 1
  • 1
Himanshu dua
  • 2,496
  • 1
  • 20
  • 27