This post was imported from my old blog “make YJL” on 2010-09-28. Some stuff in this post may be broken, please leave a comment if you see any, then I will try to fix it.
About this documentation
Since this documentation is not written by Google neither is the Python client package for Webmaster Tools API, you may encounter problems. Please leave you comments here, if they are for this documentation; for the Python client package, you can discuss at
Issue 176 or
this group.
More information you can have:
The author of this documentation and the Python client package is Yu-Jie Lin. This documentation is in the public domain.
Last updated: 2008-10-20
Getting started
Using this Python client library should only need one import:
import gdata.webmastertools.service
Authenticating to the Webmaster Tools service
All requests are sending via a GWebmasterToolsService object.
AuthSub proxy authentication
(Untested)ClientLogin username/password authentication
The following example shows how to use ClientLogin:
client = gdata.webmastertools.service.GWebmasterToolsService()client.email = '[email protected]'
client.password = '12345678'
client.source = 'Google-PythonWebmasterToolsSample-1.0')
client.ProgrammaticLogin()
Read more about
Account Authentication for Installed Applications.
Managing sites Retrieving a list of sitesTo get a list of sites, invoke
GetSitesFeed method of
GWebmasterToolsService object. For example:
feed = client.GetSitesFeed()
for entry in feed.entry:
print entry.title.text
The return value is a
SitesFeed object.
Adding a site To add a site, invoke
AddSite method of
GWebmasterToolsService object with being added site URI as the only parameter. For example:
entry = client.AddSite('http://www.example.com/')
print entry.title.text
The return value is a
SitesEntry object.
Verifying site ownershipTo verify a site's ownership, invoke
VerifySite method of
GWebmasterToolsService object with parameters, site URI and verification method. Accepted
verification methods are
htmlpage and
metatag. For example:
client.VerifySite('http://www.example.com/', 'htmlpage')
In order to verify, the requirement of verifying must be done before invoking
VerifySite method, which is creating a html page with specific name or adding a specific meta tag to the response at site URI. For what to create or add, check the
entry.verification_method in
SitesFeed.entry object.
Deleting sites
To delete a site, invoke
DeleteSite method of
GWebmasterToolsService object with being deleted site URI as the only parameter. For example:
client.DeleteSite('http://www.example.com/')
Managing site settings
Managing site settings require the site verified.
GeoLocation
Accepted locations are listed in
here. Example code:
client.UpdateGeoLocation('http://www.example.com/', 'US')
Read
More information about setting a geographic target.
Crawl rate
Accepted rates are
slower,
normal, and
faster. Example code:
client.UpdateCrawlRate('http://www.example.com/', 'normal')
Read
More information about crawl rate.
Preferred domainAccepted preferred domain setting are
none (default),
preferwww (www.example.com) and
prefernowww (http://example.com). Example code:
client.UpdatePreferredDomain('http://www.example.com/', 'preferwww')
Read
More information about setting your preferred domain.
Enhanced image search Accepted settings are
true and
false. Example code:
client.UpdateEnhancedImageSearch('http://www.example.com/', 'true')
Managing Sitemaps
Retrieving a list of submitted SitemapsTo get a list of sitemaps of a site, invoke
GetSitemapsFeed method of
GWebmasterToolsService object with site URI as the only parameter. For example:
feed = client.GetSitemapsFeed('http://www.example.com/')
for entry in feed.entry:
print entry.title.text
The return value is a
SitemapsFeed object.
Adding Sitemaps
Add a regular sitemap
To add a regular sitemap to a site, invoke
AddSitemap method of
GWebmasterToolsService object with parameters, site URI and being added sitemap URI. For example:
entry = client.AddSitemap('http://www.example.com/', 'http://www.example.com/sitemap-index.xml')
print entry.title.text
The return value is a
SitemapsEntry object.
Add a mobile sitemap
(Broken) To add a regular sitemap to a site, invoke
AddMobileSitemap method of
GWebmasterToolsService object with parameters, site URI, being added mobile sitemap URI, and format name. Accepted format name are XHTML, WML, and cHTML. For example:
entry = client.AddMobileSitemap('http://www.example.com/', 'http://www.example.com/sitemap-mobile-example.xml', 'XHTML')
The return value is a
SitemapsEntry object.
Read
More information about Mobile Sitemaps.
Add a news sitemap
(Untested) To add a news sitemap to a site, invoke
AddNewsSitemap method of
GWebmasterToolsService object with parameters, site URI, being added news sitemap URI, and publication label(s). Publication label can be a string or a list of strings. For example:
entry = client.AddNewsSitemap('http://www.example.com/', 'http://www.example.com/sitemap-news-example.xml', 'Label')
The return value is a
SitemapsEntry object.
Read
More information about News Sitemaps.
Deleting Sitemaps
To delete a sitemap from a site, invoke
DeleteSitemap method of
GWebmasterToolsService object with parameters, site URI, being deleted sitemap URI. For example:
client.DeleteSitemap('http://www.example.com/', 'http://www.example.com/sitemap-index.xml')