Skip to content

Instantly share code, notes, and snippets.

@QiZ213
Last active May 3, 2016 19:27
Show Gist options
  • Save QiZ213/696fcd5cd2ec5bae3c732e4ab4716819 to your computer and use it in GitHub Desktop.
Save QiZ213/696fcd5cd2ec5bae3c732e4ab4716819 to your computer and use it in GitHub Desktop.
import pandas as pd
US = pd.read_csv('C:/Users/Qi/Desktop/IFA.csv')
import requests
import json
for domain in US.domain:
url = "http://api.semrush.com/management/v1/projects"
querystring = {"key":"*"}
print domain
payload = "{\"project_name\":\"aaIFAtest\",\"url\":\"" + domain + "\"}"
print payload
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "75641036-7573-4abb-f383-b8f6e3671900"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
data = json.loads(response.text)
projectid = data["project_id"]
print projectid
url = "http://api.semrush.com/management/v1/projects/"+str(projectid)+"/siteaudit/enable"
querystring = {"key":"*"}
# Change crawl pages
payload = "{\r\n\t\"domain\": \"" + domain + "\",\r\n\t\"scheduleDay\":0,\"notify\":false,\"pageLimit\":100,\"userAgentType\":2\r\n\t}\r\n\t\r\n"
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "01616c54-8732-9d89-d1dd-c4a1b312f1d4"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print response.text
# run site audit
url = "https://api.semrush.com/reports/v1/projects/"+str(projectid)+"/siteaudit/launch"
querystring = {"key":"*"}
headers = {
'cache-control': "no-cache",
'postman-token': "963aaefa-6f4e-734f-8b73-219694e6223b"
}
response = requests.request("POST", url, headers=headers, params=querystring)
print response.text
data = json.loads(response.text)
snapshotid = data["snapshot_id"]
# Find Competitor
url = "http://api.semrush.com/"
querystring = {"type":"domain_organic_organic","key":"*","display_limit":"4","export_columns":"Dn","domain": domain,"database":"us"}
headers = {
'cache-control': "no-cache",
'postman-token': "479c9966-151f-07d3-0a64-438085ff2cbd"
}
response = requests.request("GET", url, headers=headers, params=querystring)
competitor = response.text
# Find Keyword
url = "http://api.semrush.com/"
querystring = {"type":"domain_organic","key":"*","display_limit":"40","export_columns":"Ph","domain": domain,"database":"us"}
headers = {
'cache-control': "no-cache",
'postman-token': "0d7feb5b-417e-d7f1-6984-8edddb53285d"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
keyword = response.text
# remove duplicates
def remove_duplicates(values):
output = []
seen = set()
for value in values:
# If value has not been encountered yet,
# ... add it to both list and set.
if value not in seen:
output.append(value)
seen.add(value)
return output
keyword = remove_duplicates(keyword.splitlines())
# Add keyword competitor
url = "http://api.semrush.com/management/v1/projects"
querystring = {"key":"*"}
payload = "{\"project_id\":" + str(projectid) +", \"project_name\":\"" + domain +"\",\"competitors\":[\"" + competitor.splitlines()[1] + "\",\"" + competitor.splitlines()[2] + "\",\"" + competitor.splitlines()[3] + "\",\"" + competitor.splitlines()[4] + "\"],\"keywords\":[{\"keyword\":\"" + keyword[1] + "\"},{\"keyword\":\"" + keyword[2] + "\"},{\"keyword\":\"" + keyword[3] + "\"},{\"keyword\":\"" + keyword[4] + "\"},{\"keyword\":\"" + keyword[5] + "\"},{\"keyword\":\"" + keyword[6] + "\"},{\"keyword\":\"" + keyword[7] + "\"},{\"keyword\":\"" + keyword[8] + "\"},{\"keyword\":\"" + keyword[9] + "\"},{\"keyword\":\"" + keyword[10] +"\"},{\"keyword\":\""+ keyword[11] + "\"},{\"keyword\":\"" + keyword[12] + "\"},{\"keyword\":\"" + keyword[13] + "\"},{\"keyword\":\"" + keyword[14] + "\"},{\"keyword\":\"" + keyword[15] + "\"},{\"keyword\":\"" + keyword[16] + "\"},{\"keyword\":\"" + keyword[17] + "\"},{\"keyword\":\"" + keyword[18] + "\"},{\"keyword\":\"" + keyword[19] + "\"},{\"keyword\":\"" + keyword[20] + "\"}]}"
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "3714ef33-f606-1d46-662a-25493ceefbd6"
}
response = requests.request("PUT", url, data=payload, headers=headers, params=querystring)
print(response.text)
# enable keyword track
url = "http://api.semrush.com/management/v1/projects/"+str(projectid)+"/tracking/enable"
querystring = {"key":"*"}
payload = "{\"tracking_url\": \""+domain+"\", \"tracking_url_type\": \"rootdomain\", \"country_id\": \"2840\", \"weekly_notification\": \"0\", \"device\": \"desktop\"}"
headers = {
'cache-control': "no-cache",
'postman-token': "99bb56fb-7d6d-6bc8-7923-ead3af6e228f"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
print(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment