Skip to main content

Python

import requests
import json

def generate_token():
try:
data = {'username': 'YOUR USER', 'password': 'YOUR PASSWORD'}
api_url = "https://api.scrapingpros.com/login"
response = requests.post(api_url, json=data, headers={'Content-Type': 'application/json'})
if response.status_code == 200:
json_proxy = response.json()
token = json_proxy['token']
return token
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def create_project(token):
try:
data = {
"name": "string",
"priority": "integer",
"description": "string"
}
api_url = "https://api.scrapingpros.com/projects"
response = requests.post(api_url, json=data, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def pause_project(token, project_id):
try:
api_url = f"https://api.scrapingpros.com/projects/{project_id}/pause"
response = requests.put(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def resume_project(token, project_id):
try:
api_url = f"https://api.scrapingpros.com/projects/{project_id}/resume"
response = requests.put(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def close_project(token, project_id):
try:
api_url = f"https://api.scrapingpros.com/projects/{project_id}/close"
response = requests.put(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def create_batch(token, project_id):
try:
data = {
'project': project_id,
'name': "string",
'priority': "integer"
}
api_url = "https://api.scrapingpros.com/batches"
response = requests.post(api_url, json=data, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def append_jobs_to_batch(token, batch_id):
try:
data = {
"jobs": [{
"url": "string",
"scrap_mode": "YOUR SCRAP MODE",
"arguments": {}
}]
}
api_url = f"https://api.scrapingpros.com/batches/{batch_id}/append-jobs"
response = requests.post(api_url, json=data, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def run_batch(token, batch_id):
try:
api_url = f"https://api.scrapingpros.com/batches/{batch_id}/run"
response = requests.post(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def pause_batch(token, batch_id):
try:
api_url = f"https://api.scrapingpros.com/batches/{batch_id}/pause"
response = requests.put(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def resume_batch(token, batch_id):
try:
api_url = f"https://api.scrapingpros.com/batches/{batch_id}/resume"
response = requests.put(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def get_projects(token):
try:
api_url = "https://api.scrapingpros.com/projects"
filter_data = {
"filter_by": "String" # Either A, P, or C
}
response = requests.get(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}, json=filter_data)
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def get_projects_summary(token):
try:
api_url = "https://api.scrapingpros.com/projects/summary"
response = requests.get(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def get_credits_summary(token):
try:
api_url = "https://api.scrapingpros.com/credits/summary"
time_period = {
"since": "String" # Either "last_month" or "year_months"
}
response = requests.get(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}, json=time_period)
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def get_jobs(token, batch_id):
try:
api_url = f"https://api.scrapingpros.com/jobs?batch={batch_id}"
response = requests.get(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")

def get_data(token, job_id):
try:
api_url = f"https://api.scrapingpros.com/get_data/{job_id}"
response = requests.get(api_url, headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
})
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code} ({response.reason})")
except Exception as e:
print(f"An error occurred: {e}")