0

I have a list of keywords stored in a column in an Excel sheet. I want to do a Google search for each keyword in separate chrome tabs.

Can anyone please help me with Python code to automate it?

Zev Averbach
  • 1,044
  • 1
  • 11
  • 25
  • Rahil, wouldn't this suite your purposes without having to use a Python script? https://chrome.google.com/webstore/detail/open-multiple-urls/oifijhaokejakekmnjmphonojcfkpbbh – Zev Averbach Jul 05 '22 at 13:20
  • Does this answer your question? [Opening tabs using Webbrowser module in Python](https://stackoverflow.com/questions/26114930/opening-tabs-using-webbrowser-module-in-python) – Maurice Meyer Jul 05 '22 at 13:22
  • I noticed you just changed "URLs" to "keywords". Do you mean you want to do a Google search for each keyword? – Zev Averbach Jul 05 '22 at 13:37
  • yea precisely want to do a google search for each item in the column at once – Rahil Shaikh Jul 05 '22 at 13:49

1 Answers1

1

Rahil, say your keywords are in the "A" column of rahils_keywords.xlsx file, in the worksheet called keywords. At the shell, install this dependency:

> pip install openpyxl

Then in your text editor or Python REPL:

import webbrowser

from openpyxl import load_workbook


def google_search_keywords_from_spreadsheet(path: str, sheet: str, column: str):
    wb = load_workbook(filename=path)
    worksheet = wb[sheet]
    num_rows = worksheet.max_row

    for row_num in range(num_rows):
        cell = f"{column}{row_num + 1}"
        keyword = worksheet[cell].value
        if keyword: # skip blank cells
            url = f"https://google.com/search?q={keyword}"
            print(f'searching for keyword "{keyword}"...')
            webbrowser.open(url)

google_search_keywords_from_spreadsheet(
    path="rahils_keywords.xlsx",
    sheet="keywords",
    column="A",
)

Zev Averbach
  • 1,044
  • 1
  • 11
  • 25
  • Thanks so much Sir but The code is giving me an error. my excel sheet has only one column and in that column i have n number of urls , i want to open all those urls at once in different chrome tabs. Thanks in advance – Rahil Shaikh Jul 05 '22 at 13:48
  • Rahil, what's the error? – Zev Averbach Jul 05 '22 at 14:07
  • File "c:\Users\Rahil\Desktop\test2.py", line 4 wb = load_workbook(filename="C:\Users\Rahil\Desktop\search.xlsx") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape – Rahil Shaikh Jul 05 '22 at 14:09
  • this may not be a panacea, but make sure you're using Python3 -- Python2 is not supported anymore! – Zev Averbach Jul 05 '22 at 14:10
  • actually have a look here, you may have copy-pasted some unwanted invisible characters: https://stackoverflow.com/q/63292817/4386191 – Zev Averbach Jul 05 '22 at 14:21
  • sure i shall do that thanks for the help and i am using python 3, i shall update you about the same ASAP – Rahil Shaikh Jul 05 '22 at 14:22
  • 1
    Thank you Sir for the help! the code works!! – Rahil Shaikh Jul 05 '22 at 14:44