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

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
2. Open up your terminal and cd into the directory you saved
3. run


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("" + 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]]

Close Menu