generated from wanghaisheng/scrape-app-reviews-without-coding
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_googlesearchconsole.py
34 lines (27 loc) · 1.3 KB
/
get_googlesearchconsole.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import csv
from googlesearchconsole import GoogleSearchConsole
def download_search_console_data(keywords, start_date, end_date):
# Set up Google Search Console API client
gsc = GoogleSearchConsole('credentials.json') # Replace with your credentials file path
# Define the desired search console fields
fields = ['date', 'query', 'page', 'country', 'device', 'clicks', 'impressions', 'ctr', 'position']
# Fetch search console data for the specified keywords and date range
data = gsc.query(
keywords=keywords,
start_date=start_date,
end_date=end_date,
dimensions=['date', 'query', 'page', 'country', 'device'],
row_limit=-1 # Fetch all available rows
)
# Save the data to a CSV file
output_file = 'search_console_data.csv'
with open(output_file, 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fields)
writer.writeheader()
writer.writerows(data)
print(f"Search Console data downloaded and saved to {output_file}")
# Example usage:
keywords = ['keyword1', 'keyword2', 'keyword3']
start_date = '2022-01-01' # Modify the start date as per your requirements
end_date = '2022-12-31' # Modify the end date as per your requirements
download_search_console_data(keywords, start_date, end_date)