Loading...
Loading...
Compare original and translation side by side
ahrefs-pythonahrefs-pythonpip install git+https://github.com/ahrefs/ahrefs-python.githttpxpydanticpip install git+https://github.com/ahrefs/ahrefs-python.githttpxpydanticahrefs-pythonhttpxrequestsYYYY-MM-DD"2025-01-15"selectwithasync withAHREFS_API_KEYahrefs-pythonhttpxrequestsYYYY-MM-DD"2025-01-15"selectwithasync withAHREFS_API_KEYimport os
from ahrefs import AhrefsClient
with AhrefsClient(api_key=os.environ["AHREFS_API_KEY"]) as client:
data = client.site_explorer_domain_rating(target="ahrefs.com", date="2025-01-15")
print(data.domain_rating) # 91.0
print(data.ahrefs_rank) # 3import os
from ahrefs import AhrefsClient
with AhrefsClient(api_key=os.environ["AHREFS_API_KEY"]) as client:
data = client.site_explorer_domain_rating(target="ahrefs.com", date="2025-01-15")
print(data.domain_rating) # 91.0
print(data.ahrefs_rank) # 3import os
import ahrefs
with ahrefs.AhrefsClient(
api_key=os.environ["AHREFS_API_KEY"],
base_url="...", # override API base URL (default: https://api.ahrefs.com/v3)
timeout=30.0, # request timeout in seconds (default: 60)
max_retries=3, # retries on transient errors (default: 2)
) as client:
...import os
from ahrefs import AsyncAhrefsClient
async with AsyncAhrefsClient(api_key=os.environ["AHREFS_API_KEY"]) as client:
data = await client.site_explorer_domain_rating(target="ahrefs.com", date="2025-01-15")import os
import ahrefs
with ahrefs.AhrefsClient(
api_key=os.environ["AHREFS_API_KEY"],
base_url="...", # 覆盖API基础URL(默认:https://api.ahrefs.com/v3)
timeout=30.0, # 请求超时时间(秒,默认:60)
max_retries=3, # 临时错误重试次数(默认:2)
) as client:
...import os
from ahrefs import AsyncAhrefsClient
async with AsyncAhrefsClient(api_key=os.environ["AHREFS_API_KEY"]) as client:
data = await client.site_explorer_domain_rating(target="ahrefs.com", date="2025-01-15")undefinedundefined
Method names follow `{api_section}_{endpoint}`, e.g. `site_explorer_organic_keywords`, `keywords_explorer_overview`.
方法名称遵循`{api模块}_{端点}`的格式,例如`site_explorer_organic_keywords`、`keywords_explorer_overview`。Nonedata = client.site_explorer_domain_rating(target="ahrefs.com", date="2025-01-15")
print(data.domain_rating)limitselectitems = client.site_explorer_organic_keywords(
target="ahrefs.com",
date="2025-01-15",
select="keyword,volume,best_position",
order_by="volume:desc",
limit=10,
)
for item in items:
print(item.keyword, item.volume, item.best_position)Nonedata = client.site_explorer_domain_rating(target="ahrefs.com", date="2025-01-15")
print(data.domain_rating)limitselectitems = client.site_explorer_organic_keywords(
target="ahrefs.com",
date="2025-01-15",
select="keyword,volume,best_position",
order_by="volume:desc",
limit=10,
)
for item in items:
print(item.keyword, item.volume, item.best_position)import ahrefs
try:
data = client.site_explorer_domain_rating(target="example.com", date="2025-01-15")
except ahrefs.AuthenticationError: # 401
...
except ahrefs.RateLimitError as e: # 429 -- e.retry_after has the delay
...
except ahrefs.NotFoundError: # 404
...
except ahrefs.APIError as e: # other 4xx/5xx -- e.status_code, e.response_body
...
except ahrefs.APIConnectionError: # network / timeout
...ahrefs.AhrefsErrorimport ahrefs
try:
data = client.site_explorer_domain_rating(target="example.com", date="2025-01-15")
except ahrefs.AuthenticationError: # 401 认证错误
...
except ahrefs.RateLimitError as e: # 429 请求超限 -- e.retry_after 包含重试延迟时间
...
except ahrefs.NotFoundError: # 404 资源不存在
...
except ahrefs.APIError as e: # 其他4xx/5xx错误 -- e.status_code、e.response_body 包含错误详情
...
except ahrefs.APIConnectionError: # 网络/超时错误
...ahrefs.AhrefsError| Parameter | Type | Description |
|---|---|---|
| | Domain, URL, or path to analyze |
| | Date in YYYY-MM-DD format |
| | Date range for history endpoints |
| | Two-letter country code (ISO 3166-1 alpha-2) |
| | Comma-separated columns to return |
| | Filter expression |
| | Column and direction, e.g. |
| | Max results to return |
wherejson.dumps()import json
where = json.dumps({"field": "volume", "is": ["gte", 1000]})
items = client.site_explorer_organic_keywords(
target="ahrefs.com", date="2025-01-15",
select="keyword,volume", where=where,
)references/filter-syntax.md| 参数 | 类型 | 描述 |
|---|---|---|
| | 要分析的域名、URL或路径 |
| | 日期,格式为YYYY-MM-DD |
| | 历史数据端点的日期范围 |
| | 两位字母国家代码(ISO 3166-1 alpha-2) |
| | 要返回的列,以逗号分隔 |
| | 过滤表达式 |
| | 排序列和方向,例如 |
| | 要返回的最大结果数 |
wherejson.dumps()import json
where = json.dumps({"field": "volume", "is": ["gte", 1000]})
items = client.site_explorer_organic_keywords(
target="ahrefs.com", date="2025-01-15",
select="keyword,volume", where=where,
)references/filter-syntax.mdreferences/api-methods.mdreferences/api-methods.md