Here is an example of how to use the SEMrush API with Python:
python
from python_semrush.semrush import SemrushClient
# Set your SEMrush API key
api_key = 'your_semrush_api_key'
# Create a SEMrushClient instance
client = SemrushClient(key=api_key)
# Get domain analytics for example.com
domain_analytics = client.domain_analytics(domain='example.com')
# Print the top 5 organic keywords
print("Top 5 Organic Keywords:")
for keyword in domain_analytics.organic_keywords[:5]:
print(f"- {keyword.keyword} (Position: {keyword.position})")
# Get backlink data for example.com
backlinks = client.backlinks(domain='example.com')
# Print the top 5 backlink sources
print("\nTop 5 Backlink Sources:")
for backlink in backlinks.backlinks[:5]:
print(f"- {backlink.domain}")
# Run a site audit for example.com
site_audit = client.site_audit(domain='example.com')
# Print the overall site audit score
print(f"\nSite Audit Score: {site_audit.audit_score}")
This code demonstrates a few key things:
1. Importing the `SemrushClient` class from the `python_semrush` library.
2. Setting your SEMrush API key.
3. Creating a `SemrushClient` instance and using it to make various API calls:
- `domain_analytics()` to retrieve domain-level analytics data.
- `backlinks()` to retrieve backlink data.
- `site_audit()` to run a site audit.
4. Printing out some of the retrieved data, such as the top organic keywords, top backlink sources, and the overall site audit score.
The `python_semrush` library provides a convenient wrapper around the SEMrush API, allowing you to make API calls and handle the responses in a Pythonic way. You can explore the various API endpoints and their corresponding methods in the library's documentation.
Remember to replace `'your_semrush_api_key'` with your actual SEMrush API key to make the API calls work.
Citations:[1] https://github.com/storerjeremy/python-semrush
[2] https://www.semrush.com/blog/beginners-guide-to-semrush-project-api/
[3] https://pipedream.com/apps/semrush/integrations/python
[4] https://www.semrush.com/kb/5-api
[5] https://www.youtube.com/watch?v=yoYn0kLyXXw