Documentation
¶
Overview ¶
Package selector 提供 HTML/XML 文档的 CSS 和 XPath 选择器。
概述 ¶
selector 包是 scrapy-go 框架的 HTML 解析核心,提供链式调用的选择器 API。 对应 Scrapy Python 版本中 parsel 库(Selector/SelectorList)的功能。 底层使用 goquery(CSS 选择器)和 htmlquery(XPath 选择器)。
核心类型 ¶
使用方式 ¶
从 HTML 字节创建 Selector 并执行查询:
sel := selector.NewFromBytes(htmlBody)
// CSS 选择器
titles := sel.CSS("h1.title").GetAll()
firstLink := sel.CSS("a").Get("")
// XPath 选择器
quotes := sel.XPath("//div[@class='quote']").GetAll()
href := sel.XPath("//a/@href").Get("")
// 属性提取
links := sel.CSSAttr("a", "href").GetAll()
// 链式嵌套查询
sel.CSS("div.item").CSS("span.price").GetAll()
通常不需要直接使用本包,而是通过 [Response] 的快捷方法:
// 在 Spider 回调中
titles := response.CSS("h1.title::text").GetAll()
links := response.XPath("//a/@href").GetAll()
Scrapy 伪元素支持 ¶
本包支持 Scrapy 风格的 CSS 伪元素语法:
- ::text — 提取元素的文本内容(通过 List.GetAll 获取)
- ::attr(name) — 提取元素属性(通过 Selector.CSSAttr 方法)
示例:
// Scrapy 风格(通过 parsePseudo 内部解析)
sel.CSS("span.text::text").GetAll()
// Go 惯用风格(推荐)
sel.CSS("span.text").GetAll() // 获取文本
sel.CSSAttr("a", "href").GetAll() // 获取属性
与 Scrapy/parsel 的差异 ¶
- 使用 goquery(基于 Go 标准库 net/html)替代 lxml/cssselect
- 使用 htmlquery(基于 Go 标准库 net/html)替代 lxml.etree
- List 是 []*Selector 类型别名,而非独立类型(更符合 Go 惯例)
- ::attr(name) 伪元素通过独立的 Selector.CSSAttr 方法实现(类型更安全)
- 解析失败时返回空 Selector(不 panic),保证链式调用安全
- 无 re() / re_first() 方法——Go 中直接使用 regexp 包处理
性能说明 ¶
每次调用 CSS/XPath 方法都会执行一次查询。如果需要对同一文档执行多次查询, 建议复用 Selector 实例而非重复创建:
sel := selector.NewFromBytes(body) // 只解析一次
titles := sel.CSS("h1").GetAll()
links := sel.CSSAttr("a", "href").GetAll()
Index ¶
- type List
- func (sl List) Attr(name string) string
- func (sl List) AttrAll(name string) []string
- func (sl List) CSS(query string) List
- func (sl List) CSSAttr(query, attr string) List
- func (sl List) First() *Selector
- func (sl List) Get(defaultVal string) string
- func (sl List) GetAll() []string
- func (sl List) Len() int
- func (sl List) XPath(expr string) List
- type Selector
- func (s *Selector) Attr(name string) (string, bool)
- func (s *Selector) CSS(query string) List
- func (s *Selector) CSSAttr(query, attr string) List
- func (s *Selector) Get(defaultVal string) string
- func (s *Selector) GetHTML() string
- func (s *Selector) HasClass(class string) bool
- func (s *Selector) XPath(expr string) List
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type List ¶
type List []*Selector
List 是 Selector 的切片,支持批量操作。
对应 Scrapy 的 List 类。
func (List) CSS ¶
CSS 对列表中每个 Selector 执行 CSS 查询,合并结果。
Example ¶
ExampleList_CSS 演示 List 的链式查询。
package main
import (
"fmt"
"github.com/dplcz/scrapy-go/pkg/selector"
)
func main() {
html := []byte(`<html><body>
<div class="product">
<span class="name">Product A</span>
<span class="price">$10</span>
</div>
<div class="product">
<span class="name">Product B</span>
<span class="price">$20</span>
</div>
</body></html>`)
sel := selector.NewFromBytes(html)
// 链式查询:先选择所有产品,再从中提取价格
prices := sel.CSS("div.product").CSS("span.price").GetAll()
fmt.Println("Prices:", prices)
// 获取第一个产品名称
firstName := sel.CSS("div.product").CSS("span.name").Get("")
fmt.Println("First:", firstName)
}
Output: Prices: [$10 $20] First: Product A
type Selector ¶
type Selector struct {
// contains filtered or unexported fields
}
Selector 表示一个 HTML/XML 选择器,支持 CSS 和 XPath 查询。
对应 Scrapy 的 Selector 类。Selector 可以从 HTML 文本或节点创建, 支持链式调用进行嵌套查询。
func NewFromBytes ¶
NewFromBytes 从 HTML 字节切片创建 Selector。
Example ¶
ExampleNewFromBytes 演示从 HTML 字节创建 Selector 并执行查询。
package main
import (
"fmt"
"github.com/dplcz/scrapy-go/pkg/selector"
)
func main() {
html := []byte(`<html><body>
<h1 class="title">Hello World</h1>
<ul>
<li class="item">Item 1</li>
<li class="item">Item 2</li>
<li class="item">Item 3</li>
</ul>
</body></html>`)
sel := selector.NewFromBytes(html)
// CSS 选择器
title := sel.CSS("h1.title").Get("")
fmt.Println("Title:", title)
// 获取所有列表项
items := sel.CSS("li.item").GetAll()
fmt.Println("Items:", items)
}
Output: Title: Hello World Items: [Item 1 Item 2 Item 3]
func (*Selector) CSS ¶
CSS 使用 CSS 选择器查询,返回匹配的 List。
支持 goquery 扩展的伪类选择器:
- 标准 CSS 选择器(如 "div.class", "#id", "a[href]")
注意:Scrapy 的 ::text 和 ::attr(name) 伪元素在 Go 中通过 List.GetAll() 和 Selector.Attr() 方法实现。
用法:
sel.CSS("div.quote") // 选择所有 class="quote" 的 div
sel.CSS("span.text").GetAll() // 获取所有匹配元素的文本
sel.CSS("a").Attr("href") // 获取第一个 <a> 的 href 属性
func (*Selector) CSSAttr ¶
CSSAttr 使用 CSS 选择器查询并提取指定属性值。 这是 Scrapy 中 css("a::attr(href)") 的 Go 等价实现。
用法:
links := sel.CSSAttr("a", "href").GetAll()
Example ¶
ExampleSelector_CSSAttr 演示使用 CSS 选择器提取属性。
package main
import (
"fmt"
"github.com/dplcz/scrapy-go/pkg/selector"
)
func main() {
html := []byte(`<html><body>
<a href="/page/1">Page 1</a>
<a href="/page/2">Page 2</a>
<a href="/page/3">Page 3</a>
</body></html>`)
sel := selector.NewFromBytes(html)
// 提取所有链接的 href 属性
links := sel.CSSAttr("a", "href").GetAll()
fmt.Println("Links:", links)
// 获取第一个链接
first := sel.CSSAttr("a", "href").Get("")
fmt.Println("First:", first)
}
Output: Links: [/page/1 /page/2 /page/3] First: /page/1
func (*Selector) Get ¶
Get 返回 Selector 的文本内容。 如果 Selector 为空,返回 defaultVal。
对于文本 Selector,直接返回文本值。 对于元素 Selector,返回元素的内部文本。
func (*Selector) XPath ¶
XPath 使用 XPath 表达式查询,返回匹配的 List。
用法:
sel.XPath("//div[@class='quote']") // 选择所有 class="quote" 的 div
sel.XPath("//span[@class='text']/text()") // 获取文本节点
sel.XPath("//a/@href") // 获取 href 属性
Example ¶
ExampleSelector_XPath 演示使用 XPath 选择器。
package main
import (
"fmt"
"github.com/dplcz/scrapy-go/pkg/selector"
)
func main() {
html := []byte(`<html><body>
<div class="quote">
<span class="text">Quote 1</span>
<span class="author">Author 1</span>
</div>
<div class="quote">
<span class="text">Quote 2</span>
<span class="author">Author 2</span>
</div>
</body></html>`)
sel := selector.NewFromBytes(html)
// XPath 查询
texts := sel.XPath("//span[@class='text']").GetAll()
fmt.Println("Texts:", texts)
authors := sel.XPath("//span[@class='author']").GetAll()
fmt.Println("Authors:", authors)
}
Output: Texts: [Quote 1 Quote 2] Authors: [Author 1 Author 2]