Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Give example code for SEMrush API to compare my website's performance with competitors


Give example code for SEMrush API to compare my website's performance with competitors


Here's an example of how you can use the SEMrush API with Python to compare your website's performance against competitors:

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)

# Domains to compare (your domain and competitors)
domains = ['your-domain.com', 'competitor1.com', 'competitor2.com']

# Get domain analytics for each domain
domain_analytics = [client.domain_analytics(domain=d) for d in domains]

# Print the top 5 organic keywords for each domain
for i, da in enumerate(domain_analytics):
    print(f"Top 5 Organic Keywords for {domains[i]}:")
    for keyword in da.organic_keywords[:5]:
        print(f"- {keyword.keyword} (Position: {keyword.position})")
    print()

# Get backlink data for each domain
backlinks = [client.backlinks(domain=d) for d in domains]

# Print the number of backlinks for each domain
for i, bl in enumerate(backlinks):
    print(f"Number of Backlinks for {domains[i]}: {bl.total_results}")

# Get traffic data for each domain
traffic = [client.domain_traffic(domain=d) for d in domains]

# Print the monthly visits for each domain
for i, t in enumerate(traffic):
    print(f"Monthly Visits for {domains[i]}: {t.visits}")

This code demonstrates how to:

1. Set up the SEMrushClient with your API key.
2. Define the domains you want to compare (your domain and competitors).
3. Use the `domain_analytics()` method to retrieve domain-level analytics for each domain, including their top organic keywords and rankings.
4. Use the `backlinks()` method to get the total number of backlinks for each domain.
5. Use the `domain_traffic()` method to retrieve the estimated monthly visits for each domain.

By comparing the organic keyword rankings, backlink profiles, and traffic data between your domain and your competitors, you can identify areas where you are performing well and areas where you need to improve to stay competitive in your market.

Remember to replace `'your_semrush_api_key'` with your actual SEMrush API key and `'your-domain.com'`, `'competitor1.com'`, and `'competitor2.com'` with your actual domain names and competitors.

Citations:
[1] https://www.semrush.com/features/competitor-website-analysis-tools/
[2] https://github.com/silktide/semrush-api
[3] https://www.semrush.com/kb/954-compare-your-competitors-performance
[4] https://developer.semrush.com/api/
[5] https://www.javatpoint.com/semrush-api-documentation