Created
April 20, 2019 18:37
-
-
Save hamletbatista/ba6cb424624ecc0a7ad9926f6790cc66 to your computer and use it in GitHub Desktop.
Get branded SEO data using the Semrush API
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}"): | |
global key | |
url_params={"type": "domain_organic", | |
"key": key, | |
"display_filter": display_filter.format(brand=brand), | |
"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(r.status_code)) | |
return None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment