Skip to main content

统一多引擎的增强型 HTTP 客户端,内置重试、选择器、信号中间件

Project description

hs-net

统一多引擎的增强型 HTTP 客户端,内置重试、选择器、信号中间件,同步异步全支持。

特性

  • 多引擎切换 — httpx、aiohttp、curl-cffi、requests、requests-go,统一 API,一行切换
  • 同步 & 异步Net(异步)和 SyncNet(同步),接口完全一致
  • 智能选择器 — 内置 CSS、XPath、正则、JMESPath 四种数据提取
  • 自动重试 — 基于 tenacity,可配置次数、间隔、随机抖动
  • 信号中间件 — 请求前、响应后、重试时三个钩子
  • 反爬支持 — curl-cffi 浏览器 TLS 指纹模拟 + 随机 User-Agent

安装

pip install hs-net

Python >= 3.10,所有引擎开箱即用,无需额外安装。

快速开始

异步

import asyncio
from hs_net import Net

async def main():
    async with Net() as net:
        resp = await net.get("https://example.com")
        print(resp.css("title::text").get())  # Example Domain

asyncio.run(main())

同步

from hs_net import SyncNet

with SyncNet() as net:
    resp = net.get("https://example.com")
    print(resp.css("title::text").get())  # Example Domain

引擎切换

# httpx(默认)
Net(engine="httpx")

# aiohttp
Net(engine="aiohttp")

# curl-cffi(支持浏览器指纹模拟)
Net(engine="curl_cffi", engine_options={"impersonate": "chrome120"})

# requests(仅同步)
SyncNet(engine="requests")

# requests-go
Net(engine="requests_go")

数据提取

resp = await net.get("https://example.com")

# CSS 选择器
resp.css("title::text").get()

# XPath
resp.xpath("//h1/text()").get()

# 正则
resp.re_first(r"价格: (\d+)元")

# JMESPath(JSON 响应)
resp = await net.get("https://api.example.com/users")
resp.jmespath("data[?age > `18`].name")

配置

from hs_net import Net, NetConfig

# 方式 1:构造函数参数
net = Net(
    engine="httpx",
    base_url="https://api.example.com/v1",
    timeout=30.0,
    retries=5,
    retry_delay=1.0,
    user_agent="chrome",
    verify=False,
    concurrency=10,
)

# 方式 2:NetConfig 对象
config = NetConfig(
    engine="curl_cffi",
    retries=3,
    user_agent="random",
    engine_options={"impersonate": "chrome120"},
)
net = Net(config=config)

参数优先级:请求方法参数 > 构造函数参数 > NetConfig 默认值

信号中间件

async with Net() as net:

    @net.on_request_before
    async def add_auth(req_data):
        req_data.headers["Authorization"] = "Bearer token"
        return req_data

    @net.on_response_after
    async def log_response(resp):
        print(f"{resp.status_code} {resp.url}")

    @net.on_request_retry
    async def on_retry(exc):
        print(f"重试: {exc}")

    resp = await net.get("https://example.com")

错误处理

from hs_net import Net, StatusException, RetryExhausted, RequestException

async with Net() as net:
    try:
        resp = await net.get("https://httpbin.org/status/404")
    except RetryExhausted as e:
        print(f"{e.attempts} 次重试失败: {e.last_exception}")
    except StatusException as e:
        print(f"HTTP {e.code}")
    except RequestException as e:
        print(f"请求异常: {e}")

文档

完整文档见 docs/ 目录,本地预览:

cd docs
pnpm install
pnpm run dev

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hs_net-0.1.0.tar.gz (314.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hs_net-0.1.0-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

File details

Details for the file hs_net-0.1.0.tar.gz.

File metadata

  • Download URL: hs_net-0.1.0.tar.gz
  • Upload date:
  • Size: 314.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hs_net-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b991d192227cd310f4db59a23dc00dd801d07eb399a733e757a61ca17abc5194
MD5 677ab705d1ebf2c62c402b07becee19d
BLAKE2b-256 c8a4438e2f79b6ace955378bd7f14024401c7c0525615cc92322aae8569f40f8

See more details on using hashes here.

File details

Details for the file hs_net-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hs_net-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 30.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hs_net-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f66b4c2bed18b13bd9d4813ea66a214bcbe3a5fa523200779eeda3cebdbe06b1
MD5 3b93bed7c29313866ab1bf719b5fa167
BLAKE2b-256 58762af011fc29c2556f7b3514ce0f5b133026694a181179eaff7a89cf980bf9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page