So last time we made our first SEO TOOL BOT class with Selenium Webdriver Firefox. We told the bot to go to an anonymous redirector (kproxy) with it’s first function. Now that’s ok. It can save you a few clicks, but we want to further automate the boring stuff. So, lets define a few more classes for our bot and pass some class wide variables through them.
Here is our previous Python code with Selenium Web Driver.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | print("*" * 60) print("MI PYTHON COM SEO TOOL BOT") print("*" * 60) from selenium import webdriver from selenium.webdriver.common.keys import Keys import time class SEO_BOT(object): def __init__(self,browser,anon_url): self.browser = browser self.anon_url = anon_url def main(self): pass def go_anon(self): ### SELENIUM IMPLICIT WAIT !!! IMPORTANT !!! self.browser.implicitly_wait(300) print("GETTING " + str(self.anon_url)) ### GO TO ANON URL self.browser.get(self.anon_url) bot = SEO_BOT(webdriver.Firefox(),"https://www.kproxy.com") bot.go_anon() |
What else should we have our Bot do? Well knowing it’s current URL would be handy. Lets define a function for that called “get location”. For this we are using another Selenium method current_url.
First we need to initialize another class wide variable in our class.
1 2 3 4 5 6 7 8 | class SEO_BOT(object): def __init__(self,browser,anon_url,current_url): self.browser = browser self.anon_url = anon_url self.current_url = None def main(self): pass |
We added the variable “current_url” to tell us where we are. We then made it available to the entire class with self. Since we haven’t gone anywhere yet we are setting the value of the current URL to None. We will update this variable when we go some where.
Here’s the function “Get Location”. Notice when we are printing we need to convert the data type to a string in many instances. We do this with the str (method?). This saves some headache when we concatenate variables as well. We are telling the Selenium Web Driver to print the current url when we call the get location function.
1 2 3 4 5 6 | def get_location(self): self.current_ip = self.browser.current_url print("*" * 60) print("CURRENT LOCATION:") print(str(self.current_ip)) print("*" * 60) |
Now lets put everything back together and run what we got. Code so far should look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | print("*" * 60) print("MI PYTHON COM SEO TOOL BOT") print("*" * 60) from selenium import webdriver from selenium.webdriver.common.keys import Keys import time class SEO_BOT(object): def __init__(self,browser,anon_url,current_url): self.browser = browser self.anon_url = anon_url self.current_url = None def main(self): pass def go_anon(self): ### SELENIUM IMPLICIT WAIT !!! IMPORTANT !!! self.browser.implicitly_wait(300) print("GETTING " + str(self.anon_url)) ### GO TO ANON URL self.browser.get(self.anon_url) def get_location(self): self.current_ip = self.browser.current_url print("*" * 60) print("CURRENT LOCATION:") print(str(self.current_ip)) print("*" * 60) bot = SEO_BOT(webdriver.Firefox(),"https://www.kproxy.com") bot.get_location() bot.go_anon() bot.get_location() |
The above should perform a location check. It should return none because we haven’t gone any where yet. Then we execute the go anon function and get our current URL then.
Cool so now lets go some where. We are going to find a page to scrape.