0
\$\begingroup\$

I am using the following script to read search terms from a file and then search those terms in google (each search result in it's own tab).

from selenium import webdriver

browser = webdriver.Chrome()

# first tab
browser.get('https://google.com')

with open("google-search-terms.adoc") as fin:
    for line_no, line in enumerate(fin):
        line = line.strip()
        line = line.replace(' ', '+')
        line = line.replace('&', '%26')
        browser.execute_script(
            "window.open('https://www.google.com/search?q="+line+"');")

How can I make this code better?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ There is not much to improve here since this code is so short. On the last line I would use an F-string. If we could look at the whole program and better understand the general purpose, then maybe we could suggest a better way of doing things. Here you use enumerate but variable line_no does not seem to be used. Either it is used further in the program or it is useless. Without looking at the big picture, hard to tell. Surely, there has to be more than that and you are building a scraping tool of some sort ? \$\endgroup\$
    – Kate
    Oct 4, 2020 at 15:19

2 Answers 2

3
\$\begingroup\$

Functions

Create individual smaller functions doing a single task.

Enumerations

The enumeration for file does not make sense. line_no servers no purpose.

if __name__ block

For scripts, it is a good practice to put your executable feature inside the if __name__ == "__main__" clause.

A rewrite of the same code might look like:

from selenium import webdriver

FILE_NAME = "google-search-terms.adoc"

def search_google(browser, term: str):
    term = term.replace(' ', '+').replace('&', '%26')
    browser.execute_script(f"window.open('https://www.google.com/search?q={term}');")


def process_file(filename: str) -> str:
    with open(filename) as fin:
        for line in fin:
            yield line.strip()


def main():
    browser = webdriver.Chrome()
    browser.get('https://google.com')
    for search_term in process_file(FILE_NAME):
        search_google(browser, search_term)


if __name__ == "__main__":
    main()
\$\endgroup\$
0
3
\$\begingroup\$
line = line.replace(' ', '+')
line = line.replace('&', '%26')
browser.execute_script(
    "window.open('https://www.google.com/search?q="+line+"');")

You can instead use urllib.parse.urlencode. This has the benefit of correctly escaping everything, for example you don't escape = to %3D.

import urllib.parse


def search_google(browser, term: str):
    query = urllib.parse.urlencode({'q': term})
    browser.execute_script(f"window.open('https://www.google.com/search?{query}');")
>>> import urllib.parse
>>> urllib.parse.urlencode({'q': 'hello world &=#$!?'})
'q=hello+world+%26%3D%23%24%21%3F'
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.