# HTMLGet
**Repository Path**: Manba024/HTMLGet
## Basic Information
- **Project Name**: HTMLGet
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-08-22
- **Last Updated**: 2025-08-22
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# HTMLGet - Go语言网页HTML获取库
[](https://golang.org)
[](LICENSE)
HTMLGet 是一个基于 Chromedp 的 Go 语言库,用于获取经过 JavaScript 渲染后的网页 HTML 内容。适用于需要获取动态网页内容的场景。
## 特性
- 🚀 **JavaScript 渲染支持** - 获取经过 JS 执行后的完整 HTML
- ⏱️ **可配置等待时间** - 灵活控制页面加载等待时间
- 🔧 **简单易用的 API** - 提供简单和高级两种调用方式
- 🌐 **自动协议处理** - 自动添加 http/https 协议前缀
- 🎭 **反检测机制** - 内置浏览器指纹伪装
注意:首次运行时会自动下载 Chrome 浏览器(约 150MB)。
## 快速开始
### 作为库使用
```go
package main
import (
"fmt"
"log"
"time"
"htmlget"
)
func main() {
// 方式1: 使用简单函数(推荐)
html, err := htmlget.FetchHTML("example.com")
if err != nil {
log.Fatal(err)
}
fmt.Println(html)
// 方式2: 指定等待时间
html, err = htmlget.FetchHTML("example.com", 5*time.Second)
if err != nil {
log.Fatal(err)
}
// 方式3: 使用高级选项
opts := &htmlget.FetchOptions{
WaitTime: 5 * time.Second,
Timeout: 30 * time.Second,
Headless: true, // 无头模式
}
html, err = htmlget.FetchHTMLWithOptions("example.com", opts)
if err != nil {
log.Fatal(err)
}
}
```
## API 文档
### FetchHTML
获取网页 HTML 内容的主要函数。
```go
func FetchHTML(url string, waitTime ...time.Duration) (string, error)
```
**参数:**
- `url` - 目标网址(自动添加 https:// 前缀)
- `waitTime` - 可选,等待页面加载的时间(默认 3 秒)
**返回:**
- `string` - HTML 内容
- `error` - 错误信息
### FetchHTMLWithOptions
带有更多配置选项的高级函数。
```go
func FetchHTMLWithOptions(url string, opts *FetchOptions) (string, error)
```
**FetchOptions 结构体:**
```go
type FetchOptions struct {
WaitTime time.Duration // 等待页面加载时间(默认 3 秒)
Timeout time.Duration // 总超时时间(默认 60 秒)
Headless bool // 是否无头模式(默认 true)
}
```
## 测试示例
项目提供了一个测试示例程序,位于 `example/main.go`:
```bash
# 进入示例目录
cd example
# 基本使用
go run main.go -url="baidu.com"
# 等待更长时间
go run main.go -url="github.com" -wait=5s
# 保存到文件
go run main.go -url="example.com" -o="output.html"
# 查看帮助
go run main.go -h
```
## 使用场景
- 🔍 **网页爬虫** - 获取动态渲染的页面内容
- 🧪 **XPath 测试** - 获取真实的 DOM 结构用于 XPath 调试
- 📊 **数据采集** - 从 SPA 应用获取数据
- 🎯 **自动化测试** - 获取页面快照进行测试验证
## 注意事项
1. **性能考虑**:每次调用都会启动一个新的 Chrome 实例,适合批量处理时考虑复用实例
2. **资源消耗**:Chrome 浏览器会消耗较多内存,注意服务器资源配置
3. **超时设置**:对于加载缓慢的页面,适当增加等待时间和超时时间
4. **并发限制**:建议控制并发数,避免资源耗尽
## 系统要求
- Go 1.19 或更高版本
- Chrome/Chromium 浏览器(会自动下载)
- Windows/Linux/macOS
## 依赖
- [chromedp](https://github.com/chromedp/chromedp) - Chrome DevTools Protocol 的 Go 实现
## 贡献
欢迎提交 Issue 和 Pull Request!
## 更新日志
### v1.0.0 (2025-01-22)
- 初始版本发布
- 支持 JavaScript 渲染
- 提供简单和高级两种 API
- 内置反检测机制