0

我是一名自由 Web 应用程序开发人员。我的一位客户命令我使用 google indexing API 开发一个 Web 应用程序。这样他就可以通过单击来索引网站 URL。

我用过python的任何方式都可以正常工作。但是客户端要求索引带有前缀 www 的 URL。但目前,API 正在从 URL 和索引中删除 www。

这里是代码: -

http = None
errors = []
urlNotificationMetadata = []

def upload_csv_and_send_togoogle(request):
    if request.method == "POST":
        form = FileUploadForm(request.POST, request.FILES)
        if form.is_valid():
            data_from_csv = form.cleaned_data.get("csv_file", None)
            csv = pd.read_csv(data_from_csv)
            csv[["URL"]].apply(lambda url: request_to_google(url, http))
    return render(request, "output.html", {'errors':errors, 'urlNotificationMetadata':urlNotificationMetadata})

SCOPES = ["https://www.googleapis.com/auth/indexing"]
credentials = None

def request_to_google(urls, http):
    credentials = ServiceAccountCredentials.from_json_keyfile_name("gindexer.json", scopes=SCOPES)
    http = credentials.authorize(httplib2.Http())
    ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"
    
    for u in urls:
        content = {}
        content['url'] = u.strip()
        content['type'] = "URL_UPDATED"
        json_ctn = json.dumps(content)    
        response, content = http.request(ENDPOINT, method="POST", body=json_ctn)
        result = json.loads(content.decode())
        if("error" in result):
            errors.append("Error({} - {}): {}".format(result["error"]["code"], result["error"]["status"], result["error"]["message"]))
        else:
            urlNotificationMetadata.append("urlNotificationMetadata.url: {}".format(result["urlNotificationMetadata"]["url"]))
            urlNotificationMetadata.append("urlNotificationMetadata.latestUpdate.url: {}".format(result["urlNotificationMetadata"]["latestUpdate"]["url"]))
            urlNotificationMetadata.append("urlNotificationMetadata.latestUpdate.type: {}".format(result["urlNotificationMetadata"]["latestUpdate"]["type"]))
            urlNotificationMetadata.append("urlNotificationMetadata.latestUpdate.notifyTime: {}".format(result["urlNotificationMetadata"]["latestUpdate"]["notifyTime"]))

你能告诉我用www前缀索引URL的方法吗?

4

0 回答 0