Created
July 29, 2019 11:54
-
-
Save DerekHawkins/343b26c20437e082873aa78e423cee34 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
### SERP Presence ### | |
### Load SEMRush Credentials and URLs for API call. | |
api_key = 'api key goes here' | |
service_url = 'https://api.semrush.com' | |
client_url = 'https://www.clientsite.com/' | |
### Various time manipulations for each part of the report ### | |
today = datetime.date.today() | |
first = today.replace(day=1) | |
lastMonth = first - datetime.timedelta(days=1) | |
previous_month = lastMonth.strftime("%m_%Y") | |
date_main = lastMonth.strftime("%Y-%m") | |
date_breakdown = monthrange(int(lastMonth.strftime("%Y")), int(lastMonth.strftime("%m"))) | |
day = date_breakdown[1] | |
#for search console | |
search_console_date = date_main + "-01" | |
#for backlinks | |
time_check = lastMonth.strftime("%Y-%m") | |
def build_urls(date_main): | |
params = { | |
"?type": "domain_organic", | |
'key': api_key, | |
'domain': client_url, | |
'export_columns': "Ph,Po,Nq,Vu", | |
'database': 'us', | |
'display_date': date_main, | |
'display_limit': '10000' | |
} | |
data = urllib.parse.urlencode(params, doseq=True) | |
main_call = urllib.parse.urljoin(service_url, data) | |
main_call = main_call.replace(r'%3F', r'?') | |
return main_call | |
def parse_response(call_data): | |
results = [] | |
data = call_data.decode('unicode_escape') | |
lines = data.split('\r\n') | |
lines = list(filter(bool, lines)) | |
columns = lines[0].split(';') | |
for line in lines[1:]: | |
result = {} | |
for i, datum in enumerate(line.split(';')): | |
result[columns[i]] = datum.strip('"\n\r\t') | |
results.append(result) | |
return results | |
def API_Call(url): | |
try: | |
response = requests.get(url) | |
read = response.content | |
final = parse_response(call_data=read) | |
except IndexError as e: | |
final = {} | |
return final | |
# API URL build/call and data compiling and filtering | |
frames = [] | |
call = build_urls(date_main=date_main) | |
nums = [] | |
data_set = API_Call(url=call) | |
df = pd.DataFrame(data_set) | |
df['Top'] = df['Position'].astype(int).between(0,3, inclusive=True) | |
df_1_to_3 = df[df.Top.astype(str).str.contains("True")] | |
df['Top'] = df['Position'].astype(int).between(4,10, inclusive=True) | |
df_4_to_10 = df[df.Top.astype(str).str.contains("True")] | |
df['Top'] = df['Position'].astype(int).between(11,20, inclusive=True) | |
df_11_to_20 = df[df.Top.astype(str).str.contains("True")] | |
tops = df_1_to_3.Keyword.value_counts().sum() | |
middles = df_4_to_10.Keyword.value_counts().sum() | |
bottoms = df_11_to_20.Keyword.value_counts().sum() | |
nums.append((tops, middles, bottoms)) | |
df1 = pd.DataFrame(nums, columns=['Top 3', '4 Thru 10', '11 Thru 20']) | |
frames.append(df1) | |
final_search_presence = pd.concat(frames) | |
final_search_presence['Month'] = date_main | |
final_search_presence['Top 10'] = final_search_presence['Top 3']+final_search_presence['4 Thru 10'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment