Created
April 26, 2020 00:55
-
-
Save hamletbatista/ed5e810b56acf0f8490e29050caa4351 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 requests | |
from urllib.parse import urlencode, urlparse, urlunparse, quote | |
import pandas as pd | |
#def get_seo_branded_data(brand, domain, database="us", export_columns="Ph,Po,Nq,Ur,Tg,Td,Ts", display_limit=10000, display_filter="+|Ph|Co|{brand}"): | |
#Found that SERP features is -> Fl in https://www.semrush.com/api-analytics/#columns | |
def get_serp_features(domain, database="us", export_columns="Ph,Fk", display_limit=100): | |
global key | |
url_params={"type": "domain_organic", | |
"key": key, | |
"display_limit": display_limit, | |
"export_columns": export_columns, | |
"domain": domain, | |
"database": database | |
} | |
api_url="https://api.semrush.com/" | |
qs = urlencode(url_params) | |
u = urlparse(api_url) | |
api_request = urlunparse((u.scheme, u.netloc, u.path, u.params, qs, u.fragment)) | |
#print(api_request) | |
r = requests.get(api_request) | |
if r.status_code == 200: | |
results = r.text.split("\r\n") # | |
headers = results[0].split(";") # save result headers to list | |
table = [x.split(";") for x in results[1:]] #save columns to list of lists | |
df = pd.DataFrame(table, columns=headers).dropna() #remove null types | |
return df | |
else: | |
print("API call failed with code {code}".format(code=r.status_code)) | |
return None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment