Back into the grind again. Thought I would expand the functionality of our SEO Tool Bot in Python Selenium. I noticed I’m always going to our friends at www.moonsy.com and using their awesome Google keyword rank checker for SEO purposes. This really is a good service they offer for free so definitely go over there and click around a little.
So what the hay. Lets automate this thing so we don’t have to do so much typing and plus it’s just plain cool to make the bot do it. Were gonna build on our previous SEO Tool Bot in Python and Selenium framework we started before.
I’m not gonna bore everyone with the imports we should be familiar with importing Selenium and Time. If you want a more in depth basic tutorial. Go to Tutorial one. Lets get this done. Here’s what were gonna do.
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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,scrape_page): self.browser = browser self.anon_url = anon_url self.scrape_page = scrape_page self.scrape_html = None self.current_url = None self.site_urls = site_urls ### SITE URLS TO INJECT INTO MOONSY KEYWORD RANK CHECKER KEYWORD LOOK UP FUNCTION self.key_words = key_words ### KEY WORDS TO SCRAPE ON MOONSY KEYWORD LOOK UP FUNCTION 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) ### FIND ELEMENTS AND POST SCRAPE PAGE URL elem = self.browser.find_element_by_id("maintextfield").clear() print("CLEARED maintextfield") elem = self.browser.find_element_by_id("maintextfield").send_keys(self.scrape_page) print("POSTING" + str(self.scrape_page) + "TO" + str(self.anon_url)) elem = self.browser.find_element_by_id("maintextfield").submit() print("REDIRECTING TO" + str(self.scrape_page)) def get_location(self): self.current_ip = self.browser.current_url print("*" * 60) print("CURRENT LOCATION:") print(str(self.current_ip)) print("*" * 60) def scrape(self): self.browser.implicitly_wait(300) print("FINDING ELEMENTS ON " + self.scrape_page + "TO SCRAPE") #self.scrape_html = self.browser.page_source.encode('utf-8') self.scrape_html = self.browser.page_source.encode('ascii', 'ignore') print("SCRAPPING" + str(self.scrape_page)) print(str(self.scrape_html)) print(str(self.scrape_page) + "HAS BEEN SCRAPPED") return str(self.scrape_html) def key_word_lookup(self): |
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,scrape_page): self.browser = browser self.anon_url = anon_url self.scrape_page = scrape_page self.scrape_html = None self.current_url = None self.site_urls = site_urls ### SITE URLS TO INJECT INTO MOONSY KEYWORD RANK CHECKER KEYWORD LOOK UP FUNCTION self.key_words = key_words ### KEY WORDS TO SCRAPE ON MOONSY KEYWORD LOOK UP FUNCTION 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) ### FIND ELEMENTS AND POST SCRAPE PAGE URL elem = self.browser.find_element_by_id("maintextfield").clear() print("CLEARED maintextfield") elem = self.browser.find_element_by_id("maintextfield").send_keys(self.scrape_page) print("POSTING" + str(self.scrape_page) + "TO" + str(self.anon_url)) elem = self.browser.find_element_by_id("maintextfield").submit() print("REDIRECTING TO" + str(self.scrape_page)) def get_location(self): self.current_ip = self.browser.current_url print("*" * 60) print("CURRENT LOCATION:") print(str(self.current_ip)) print("*" * 60) def scrape(self): self.browser.implicitly_wait(300) print("FINDING ELEMENTS ON " + self.scrape_page + "TO SCRAPE") #self.scrape_html = self.browser.page_source.encode('utf-8') self.scrape_html = self.browser.page_source.encode('ascii', 'ignore') print("SCRAPPING" + str(self.scrape_page)) print(str(self.scrape_html)) print(str(self.scrape_page) + "HAS BEEN SCRAPPED") return str(self.scrape_html) def key_word_lookup(self):
In the above Python Selenium code for our SEO Tool Bot. We added two new class wide variables to be initialized, “site urls” and “key_words”. We are gonna store the URLS and key words in lists respectively upon instantiation. Then loop through the data and return the results. Now lets work on our key word look up function for our SEO bot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | def key_word_lookup(self): def key_word_lookup(self): ### ClICKS ON THE ADD DOMAIN ON MOONSY X4 elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() time.sleep(2) elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() time.sleep(2) elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() time.sleep(2) elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() elem = self.browser.find_element_by_id("keyword").clear() print("CLEARED KEYWORD ELEMENT") #for kw in kws: elem = self.browser.find_element_by_id("keyword").clear() elem = self.browser.find_element_by_id("keyword").send_keys(kws) #for site in sites: #site = sites[site] elem = self.browser.find_element_by_name("domains[]").send_keys(sites["1"]) elem = self.browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/fieldset[3] /input[1]').send_keys(sites["2"]) elem = self.browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/fieldset[4]/input[1]').send_keys(sites["3"]) elem = self.browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/fieldset[5]/input[1]').send_keys(sites["4"]) |
def key_word_lookup(self): def key_word_lookup(self): ### ClICKS ON THE ADD DOMAIN ON MOONSY X4 elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() time.sleep(2) elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() time.sleep(2) elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() time.sleep(2) elem = self.browser.find_element_by_xpath('//*[@id="addDomain"]').click() elem = self.browser.find_element_by_id("keyword").clear() print("CLEARED KEYWORD ELEMENT") #for kw in kws: elem = self.browser.find_element_by_id("keyword").clear() elem = self.browser.find_element_by_id("keyword").send_keys(kws) #for site in sites: #site = sites[site] elem = self.browser.find_element_by_name("domains[]").send_keys(sites["1"]) elem = self.browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/fieldset[3] /input[1]').send_keys(sites["2"]) elem = self.browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/fieldset[4]/input[1]').send_keys(sites["3"]) elem = self.browser.find_element_by_xpath('/html/body/div[2]/div/div[2]/form/fieldset[5]/input[1]').send_keys(sites["4"])
The above script for the SEO TOOL BOT in Selenium will start interacting with the Google Rank Checker Page on Moonsy. Through the Selenium Web Driver. I’ve inspected the elements and imputed the relevant data for the Selenium Webdriver to do its magic. First the Bot will click four times on the “add domain” button then enter URL and Key Word list. After entering the data we will retrieve the relevant results for our Google Key Word Rank on Moonsy.
I was going to go with a list and loop through it with a Python for loop then I decided on a dictionary as the most Pythonic data structure for this application SEO Tool Bot in Selenium. We can change it up later if need be.
1 2 3 4 5 6 7 8 9 | kproxy = "https://www.kproxy.com" moonsy = "https://moonsy.com/google-keyword-rank-checker/" sites = {"1":"40k.co","2":"mipython.com","3":"www.magwebdesigns.net","4": "flintseo.co"} kws = ["SEO BOT","PYTHON BOT"] bot = SEO_BOT(webdriver.Firefox(),kproxy,moonsy,sites,kws) bot.go_anon() bot.key_word_lookup() |
kproxy = "https://www.kproxy.com" moonsy = "https://moonsy.com/google-keyword-rank-checker/" sites = {"1":"40k.co","2":"mipython.com","3":"www.magwebdesigns.net","4": "flintseo.co"} kws = ["SEO BOT","PYTHON BOT"] bot = SEO_BOT(webdriver.Firefox(),kproxy,moonsy,sites,kws) bot.go_anon() bot.key_word_lookup()
end