# 网页数据抓取器 (CSS Selector Scraper) (`ideal_warhead_1ua/my-actor`) Actor

按 CSS 选择器从任意公开网页提取结构化数据，结果直接写入数据集，可导出 JSON/CSV/Excel。适用于商品列表、文章列表、招聘信息、价格监控等场景。

- **URL**: https://apify.com/ideal_warhead_1ua/my-actor.md
- **Developed by:** [贺 宇乐](https://apify.com/ideal_warhead_1ua) (community)
- **Categories:** Developer tools, Automation
- **Security:** Limited permissions
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $2.00 / 1,000 results

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-event

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## 网页数据抓取器 / Generic Web Scraper

按 CSS 选择器从公开网页提取结构化数据，输出 JSON / CSV / Excel，**按成功结果计费**。

A lightweight, pay-per-result web scraper. Point it at public pages, tell it which elements to grab, and get clean structured data back.

***

### 这个 Actor 能做什么

- 抓取任意**公开**网页上的列表数据：商品列表、文章列表、职位列表、公告列表等
- 用 CSS 选择器自定义要提取的字段，无需改代码
- 结果自动写入 dataset，可导出 CSV / JSON / Excel，也可通过 API 拉取
- 内置礼貌抓取间隔、单次结果上限、消费上限保护

### 输入参数

| 参数 | 说明 |
|---|---|
| `startUrls` | 要抓取的页面地址，可填多个 |
| `itemSelector` | 定位单个条目的 CSS 选择器，如 `.athing`、`article.post` |
| `fields` | 字段映射。key 是输出字段名，`selector` 是相对条目的选择器，`attribute` 可指定取属性值（如 `href`、`src`），留空则取文本 |
| `maxItems` | 单次最多写入的结果条数，默认 100，上限 5000 |
| `requestDelaySecs` | 相邻请求间隔，默认 1 秒 |
| `dryRun` | 调试模式，开启后不计费 |
| `proxyConfiguration` | 目标站有频率限制时可开启 Apify 代理 |

### 输出示例

```json
{
  "title": "Show HN: I built a tiny database",
  "url": "https://example.com/post/123",
  "_sourceUrl": "https://news.ycombinator.com"
}
```

抓取失败的页面会写入一条带 `_error` 字段的记录，便于排查，不会中断整次运行。

### 计费方式

按 **成功写入的结果条数** 计费，另有极低的启动费。未产出数据的请求不收费。

价格为标示单价，你的 Apify 订阅等级越高，实际单价越低。

### 使用限制

- 仅支持静态 HTML 页面。需要执行 JavaScript 渲染的站点，请先确认页面源码中是否已包含目标数据
- 单次运行受内存与超时限制（512 MB – 1 GB，默认 3600 秒）
- 达到 `maxItems` 或你的消费上限时自动停止

### 合规声明

本 Actor 仅用于抓取公开可访问的网页内容。使用者需自行确认目标站点的服务条款、robots.txt 及所在地法律法规，不得用于采集个人隐私数据、绕过登录或付费墙、或任何侵犯他人权益的用途。作者不对使用者的具体使用场景承担责任。

***

**关键词 / Keywords**: web scraper, data extraction, CSS selector, structured data, JSON export, CSV export, 网页抓取, 数据采集

# Actor input Schema

## `startUrls` (type: `array`):

要抓取的公开页面地址，支持多个。请确认目标站点允许抓取其公开内容。

## `itemSelector` (type: `string`):

定位列表页中每一个条目的 CSS 选择器，例如 .athing 或 article.post

## `fields` (type: `object`):

定义要提取的字段。key 是输出字段名，selector 是相对条目的 CSS 选择器，attribute 可指定取属性值（如 href、src），留空则取文本。

## `maxItems` (type: `integer`):

限制单次运行写入的结果条数，用于控制成本。达到后自动停止。

## `requestDelaySecs` (type: `number`):

相邻请求之间的等待时间，礼貌抓取，降低被限流概率。

## `dryRun` (type: `boolean`):

开启后正常抓取并写入数据，但不触发计费。仅用于调试。

## `proxyConfiguration` (type: `object`):

如目标站点有频率限制，可开启 Apify 代理。

## Actor input object example

```json
{
  "startUrls": [
    {
      "url": "https://news.ycombinator.com"
    }
  ],
  "itemSelector": ".athing",
  "fields": {
    "title": {
      "selector": ".titleline > a"
    },
    "url": {
      "selector": ".titleline > a",
      "attribute": "href"
    }
  },
  "maxItems": 100,
  "requestDelaySecs": 1,
  "dryRun": false,
  "proxyConfiguration": {
    "useApifyProxy": false
  }
}
```

# Actor output Schema

## `dataset` (type: `string`):

JSON 数组，可追加 ?format=csv 或 ?format=xlsx 导出为表格

## `datasetCsv` (type: `string`):

直接的 CSV 下载链接，适合 Excel 打开

## `runLog` (type: `string`):

本次运行的完整日志，排查选择器失效用

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "startUrls": [
        {
            "url": "https://news.ycombinator.com"
        }
    ],
    "itemSelector": ".athing",
    "fields": {
        "title": {
            "selector": ".titleline > a"
        },
        "url": {
            "selector": ".titleline > a",
            "attribute": "href"
        }
    },
    "proxyConfiguration": {
        "useApifyProxy": false
    }
};

// Run the Actor and wait for it to finish
const run = await client.actor("ideal_warhead_1ua/my-actor").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {
    "startUrls": [{ "url": "https://news.ycombinator.com" }],
    "itemSelector": ".athing",
    "fields": {
        "title": { "selector": ".titleline > a" },
        "url": {
            "selector": ".titleline > a",
            "attribute": "href",
        },
    },
    "proxyConfiguration": { "useApifyProxy": False },
}

# Run the Actor and wait for it to finish
run = client.actor("ideal_warhead_1ua/my-actor").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "startUrls": [
    {
      "url": "https://news.ycombinator.com"
    }
  ],
  "itemSelector": ".athing",
  "fields": {
    "title": {
      "selector": ".titleline > a"
    },
    "url": {
      "selector": ".titleline > a",
      "attribute": "href"
    }
  },
  "proxyConfiguration": {
    "useApifyProxy": false
  }
}' |
apify call ideal_warhead_1ua/my-actor --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,ideal_warhead_1ua/my-actor"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/vXSVubgmLNghI8wfO/builds/FSDEllkai7zijLqPX/openapi.json
