Using Python to Quickly Check Keyword Results | | Professional SEO PPC and Marketing Agency Indianapolis | Noblesville

I wrote this quick console app in python to quickly grab results for a given keyword. Usage is fairly straight-forward:

1. Copy paste the below code into a file (name the file kwcheck.py)
2. Open up your terminal and cd into the directory you saved kwcheck.py
3. run

python kwcheck.py

4. Type your keyword and hit enter.


from urllib.parse import urlencode, urlparse, parse_qs
from lxml.html import fromstring
import csv
from requests import get


def search():
  user_query = input('type keywords to search: \n')
  raw = get("https://www.google.com/search?q=" + user_query).text
  page = fromstring(raw)
  links = page.cssselect('.r a')
  for row in links:
   raw_url = row.get('href')
   title = row.text_content()
   if raw_url.startswith("/url?"):
       url = parse_qs(urlparse(raw_url).query)['q']
       result_row = [title, url[0]]
       print(result_row)

search()
Close Menu