# configs详解——之selector

`selector`是页面元素选择器类，下面介绍此类可以调用的方法

#### `select($html, $selector, $selector_type = 'xpath')` <a href="#selecthtml-selector-selectortype--xpath" id="selecthtml-selector-selectortype--xpath"></a>

> @param $html 需筛选的网页内容\
> @param $selector 选择器规则\
> @param $selector\_type 选择器类型: xpath、regex、css, 默认为xpath选择类型

栗子1:

通过xpath选择器提取网页内容的标题

```
$html = requests::get("http://www.epooll.com/archives/806/");
$data = selector::select($html, "//div[contains(@class,'page-header')]//h1//a");
var_dump($data);
```

栗子2:

通过css选择器提取网页内容的标题

```
$html = requests::get("http://www.epooll.com/archives/806/");
$data = selector::select($html, ".page-header > h1 > a", "css");
var_dump($data);
```

栗子3:

通过正则匹配提取网页内容的标题

```
$html = requests::get("http://www.epooll.com/archives/806/");
$data = selector::select($html, '@<title>(.*?)</title>@', "regex");
var_dump($data);
```

#### `remove($html, $selector, $selector_type = 'xpath')` <a href="#removehtml-selector-selectortype--xpath" id="removehtml-selector-selectortype--xpath"></a>

> @param $html 需过滤的网页内容\
> @param $selector 选择器规则\
> @param $selector\_type 选择器类型: xpath、regex、css, 默认为xpath选择类型

举个例子:

```
$html =<<<STR
    <div id="demo">
        aaa
        <span class="tt">bbb</span>
        <span>ccc</span>
        <p>ddd</p>
    </div>
STR;

// 获取id为demo的div内容
$html = selector::select($html, "//div[contains(@id,'demo')]");
// 在上面获取内容基础上，删除class为tt的span标签
$data = selector::remove($html, "//span[contains(@class,'tt')]");
print_r($data);
```

### results matching ""

*

### No results matching ""


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zhai-shi-sansorganization.gitbook.io/phpspider/configs-xiang-jie-zhi-selector.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
