Skip to content

Instantly share code, notes, and snippets.

@observerss
Last active May 8, 2020 03:50

Revisions

  1. observerss revised this gist Oct 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # EDIT: 2013/10/20
    # google has updated its kwt UI, this script doesn't work any more!
    # may be I will update this script if when I have time to investigate their new Interface.
    # may be I will update this script when I have time to investigate their new Interface.

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
  2. observerss revised this gist Oct 21, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    # EDIT: 2013/10/20
    # google has updated its kwt UI, this script doesn't work any more!
    # may be I will update this script if when I have time to investigate their new Interface.

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    import selenium.webdriver.support.wait
  3. observerss revised this gist Oct 18, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -67,7 +67,7 @@ def find_keyword_volumes(self, keywords):
    # if we fail, fail gracefully
    pass
    else:
    text = self.ff.find_elements_by_xpath('//table[@class="sLNB"]')[0].text
    text = self.ff.find_elements_by_xpath('//table[@class="sMNB"]')[0].text
    texts = text.split('\n')
    for i in range(1,len(texts)/4):
    ret[ texts[i*4] ] = (texts[i*4+2], texts[i*4+3])
  4. observerss revised this gist Sep 28, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -77,5 +77,5 @@ def find_keyword_volumes(self, keywords):
    return ret

    if __name__ == "__main__":
    aa = AdwordsAutomater(email="[email protected]", passwd="1qaz2wsx!@")
    aa = AdwordsAutomater(email="REPLACE_THIS_ADDRESS", passwd="REPLACE_THIS_PASSWORD")
    print aa.find_keyword_volumes(["ipad","cars","a0012k2k2","kindle","mac","beats","travel"])
  5. observerss revised this gist Sep 28, 2012. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -74,4 +74,8 @@ def find_keyword_volumes(self, keywords):

    self.busy = False

    return ret
    return ret

    if __name__ == "__main__":
    aa = AdwordsAutomater(email="[email protected]", passwd="1qaz2wsx!@")
    print aa.find_keyword_volumes(["ipad","cars","a0012k2k2","kindle","mac","beats","travel"])
  6. observerss revised this gist Sep 28, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,10 @@
    import selenium.webdriver.support.wait
    selenium.webdriver.support.wait.POLL_FREQUENCY = 0.05

    import re
    import random
    import collections

    class AdwordsAutomater(object):
    def __init__(self, email, passwd):
    self.email = email
  7. observerss revised this gist Sep 28, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    import selenium.webdriver.support.wait
    selenium.webdriver.support.wait.POLL_FREQUENCY = 0.05

    class AdwordsAutomater(object):
    def __init__(self, email, passwd):
    self.email = email
  8. observerss renamed this gist Sep 28, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. observerss created this gist Sep 28, 2012.
    68 changes: 68 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    class AdwordsAutomater(object):
    def __init__(self, email, passwd):
    self.email = email
    self.passwd = passwd
    try:
    self.ff = webdriver.Chrome()
    except:
    self.ff = webdriver.Firefox()
    self.ff.set_page_load_timeout(30)
    self.ff.implicitly_wait(30)
    self.busy = False
    self.is_login = False
    self.on_keyword_page = False
    self.kwurl = ''

    def login(self):
    email, passwd = self.email, self.passwd
    try:
    print 'getting adwords.google.com'
    self.ff.get('https://adwords.google.com')
    except TimeoutException:
    pass
    self.ff.find_element_by_id("Email").send_keys(email)
    self.ff.find_element_by_id("Passwd").send_keys(passwd)
    signin = self.ff.find_element_by_id('signIn')
    try:
    print 'submit login form'
    signin.submit()
    except TimeoutException:
    pass
    self.is_login = True
    search = re.compile(r'(\?[^#]*)#').search(self.ff.current_url).group(1)
    self.kwurl = 'https://adwords.google.com/o/Targeting/Explorer'+search+'&__o=cues&ideaRequestType=KEYWORD_IDEAS';

    def find_keyword_volumes(self, keywords):
    if not self.is_login:
    self.login()

    if not isinstance(keywords, collections.Iterable):
    keywords = [ keywords ]

    print self.email, 'querying', keywords
    self.busy = True
    ret = {}

    print 'visiting keyword tools'
    self.ff.get(self.kwurl)

    kwinput = self.ff.find_element_by_class_name("sEAB")
    kwinput.send_keys('\n'.join(keywords))

    self.ff.find_element_by_css_selector("button.gwt-Button").click()

    try:
    # wait for at least one elements ready, implicitly
    self.ff.find_elements_by_xpath('//tr//*[contains(text(),"{0}")]'.format(random.choice(keywords)))
    except TimeoutException:
    # if we fail, fail gracefully
    pass
    else:
    text = self.ff.find_elements_by_xpath('//table[@class="sLNB"]')[0].text
    texts = text.split('\n')
    for i in range(1,len(texts)/4):
    ret[ texts[i*4] ] = (texts[i*4+2], texts[i*4+3])

    self.busy = False

    return ret