【问题标题】:Submit sitemaps using Python and Google Search Console API使用 Python 和 Google Search Console API 提交站点地图
【发布时间】:2018-03-22 15:45:28
【问题描述】:

我正在编写一个 Python 脚本以将站点地图连续提交到 Search Console。 Python 快速入门示例运行正常:https://developers.google.com/webmaster-tools/search-console-api-original/v3/quickstart/quickstart-python

我尝试使用以下代码提交我的站点地图:

CLIENT_ID = 'xxxxx'
CLIENT_SECRET = 'yyyyyy'

OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters'

# 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_SECRET, OAUTH_SCOPE, 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)

WEBSITE = 'http://www.mywebsite.com'
SITEMAP_PATH = 'http://www.mywebsite.com/sitemaps/'
SITEMAPS_LIST = ['sitemap1.xml','sitemap2.xml','sitemap3.xml']

print ('Adding sitemaps to website ' + WEBSITE)
for sitemap in SITEMAPS_LIST:
    print ('  '+SITEMAP_PATH + sitemap)
    webmasters_service.sitemaps().submit(siteUrl=WEBSITE, feedpath=SITEMAP_PATH + sitemap)

没有错误。但是站点地图没有添加到我的 Search Console 中。 如果我尝试在 Search Console 中或通过 API 资源管理器 Web 界面手动添加文件,它就可以正常工作。

【问题讨论】:

    标签: python google-api-python-client google-search-console xml-sitemap


    【解决方案1】:

    您可以尝试将 .execute() 添加到语句的最后吗? 喜欢

    webmasters_service.sitemaps().submit(siteUrl=WEBSITE, feedpath=SITEMAP_PATH + sitemap).execute()

    【讨论】:

      猜你喜欢
      • 2023-01-16
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 2022-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多