Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vladimirmyshkovski/e9955f5728baeb44c6df1bda2ab3e7ef to your computer and use it in GitHub Desktop.
Save vladimirmyshkovski/e9955f5728baeb44c6df1bda2ab3e7ef to your computer and use it in GitHub Desktop.
Python3 google api search
import httplib2
from apiclient import errors
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
from googleapiclient import sample_tools
# Copy your credentials from the console
CLIENT_ID = '76812769011-64bu4bg3bj7o372k4pnpgpjfekuh97mc.apps.googleusercontent.com'
CLIENT_SECRET = 'SyfFR2LRhZ2Q_ZYCiF4_LUB_'
# Check https://developers.google.com/webmaster-tools/search-console-api-original/v3/ for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly'
# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=OAUTH_SCOPE,
redirect_uri=REDIRECT_URI
)
authorize_url = flow.step1_get_authorize_url()
print('Go to the following link in your browser: ' + authorize_url)
code = input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
webmasters_service = build('webmasters', 'v3', http=http)
# Retrieve list of properties in account
site_list = webmasters_service.sites().list().execute()
# Filter for verified websites
print(site_list)
site_entry = site_list.get('siteEntry', None)
if site_entry:
verified_sites_urls = [
s['siteUrl'] for s in site_list['siteEntry'] if
s['permissionLevel'] != 'siteUnverifiedUser' and
s['siteUrl'][:4] == 'http'
]
else:
print('Verified sites urls is empty')
verified_sites_urls = []
# Printing the URLs of all websites you are verified for.
for site_url in verified_sites_urls:
print(site_url)
# Retrieve list of sitemaps submitted
sitemaps = webmasters_service.sitemaps().list(siteUrl=site_url).execute()
if 'sitemap' in sitemaps:
sitemap_urls = [s['path'] for s in sitemaps['sitemap']]
print(" " + "\n ".join(sitemap_urls))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment