0

I am very new to Python. I am trying to write a program that will take an IP address and use the search function on https://community.spiceworks.com/tools/ip-lookup/ to search that IP and return the results to me.

I wrote this:

import selenium.webdriver as webdriver

def get_results(search_term):
    url = "https://community.spiceworks.com/tools/ip-lookup/"
    browser = webdriver.Firefox()
    browser.get(url)
    search_box = browser.find_element_by_id("ipaddress")
    search_box.send_keys(search_term)
    search_box.submit()
    try:
        tables = browser.find_element_by_class("in-card detail-list ember-view")
    results = []
    for table in tables:
        print(table)
        results.append(table)
    browser.close()

    return results
    print(results)

get_results("137.2.167.117")

(I used a random IP address) When I run this, I get:

results = [] (with an arrow pointing to the "s" in results) SyntaxError: invalid syntax

Am I writing this code correctly? What is causing that error? And am I using the search function and results correctly? Searching this didn't return many results. Thanks!

2 Answers 2

1

As defined by the language a try block must be followed by an except or a finally: https://docs.python.org/3/reference/compound_stmts.html#try

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
...    print("foo")
... results = []
  File "<stdin>", line 3
    results = []
          ^
SyntaxError: invalid syntax

Your code fails for the same reason as the above snippet, the next statement (results) is neither a except or finally.

Sign up to request clarification or add additional context in comments.

Comments

0

I haven't time to try to run the code because I don't have the selenium library at the moment, but just pasting it into Pycharm highlights a couple of issues:

  • you need to define the except for your try
  • if the try fails, tables is not going to be defined and will cause issues when reading it in the for loop
  • print(results) after the return will never be reached

What's causing the syntax error is the lack of except. A quick fix for the first two issues would be:

try:
    tables = browser.find_element_by_class("in-card detail-list ember-view")
except:
    tables = []

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.