Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.