Created
July 22, 2020 04:32
-
-
Save MartijnSch/4f1696f2c539740222818cb8d5076e24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import requests | |
import csv | |
import json | |
import re | |
URL = "https://example.com" | |
API_KEY = '' | |
request_url = "https://ssl.bing.com/webmaster/api.svc/json/GetQueryStats?apikey={}&siteUrl={}".format(API_KEY, URL) | |
request = requests.get(request_url) | |
if request.status_code == 200: | |
query_data = json.loads(request.text) | |
with open("bing_query_stats_{}.csv".format(datetime.date.today()), mode='w') as new_file: | |
write_row = csv.writer(new_file, delimiter=',', quotechar='"') | |
write_row.writerow(['AvgClickPosition', 'AvgImpressionPosition', 'Clicks', 'Impressions', 'Query', 'Created', 'Date']) | |
for key in query_data["d"]: | |
# Get date | |
match = re.search('/Date\\((.*)\\)/', key["Date"]) | |
write_row.writerow([key["AvgClickPosition"] / 10, | |
key["AvgImpressionPosition"] / 10, | |
key["Clicks"], | |
key["Impressions"], | |
key["Query"], | |
datetime.datetime.now(), | |
datetime.datetime.fromtimestamp(int(match.group(1)) // 1000)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for building out the code! Is there a way to ping a Bing WMT endpoint for keyword-level stats, especially for keyword-level Impressions? And also Impressions that go back 6 months?
Reference: https://docs.microsoft.com/en-us/dotnet/api/microsoft.bing.webmaster.api.interfaces.keywordstats?view=bing-webmaster-dotnet