go语言:使用goQuery采集IP并设置请求代理

本文介绍了如何在Go语言中利用goQuery库进行网页抓取,并展示了如何采集代理IP。通过代理IP池获取代理,然后在请求中设置代理。详细介绍了goQuery的主要方法,包括Document和Selection,以及代理IP的使用示例。
摘要由CSDN通过智能技术生成

一、goQuery库:

1.简述

据官方描述,goQuery实现了与jQuery相似的DOM操作功能。与jQuery不同的是,jQuery返回的是完整的DOM树,而goQuery返回的是DOM节点。goQuery底层由golang标准库net/html实现,解析器要求文档必须是 UTF-8 编码,使用者应按需转换文档编码。goQuery-readme

2.主要方法

2.1 Document: 返回要被操作的HTML文档

// Document represents an HTML document to be manipulated. Unlike jQuery, which
// is loaded as part of a DOM document, and thus acts upon its containing
// document, GoQuery doesn't know which HTML document to act upon. So it needs
// to be told, and that's what the Document class is for. It holds the root
// document node to manipulate, and can make selections on this document.
type Document struct {
   
	*Selection
	Url      *url.URL
	rootNode *html.Node
}

2.2 Selection: 符合指定条件的节点。

// Selection represents a collection of nodes matching some criteria. The
// initial Selection can be created by using Document.Find, and then
// manipulated using the jQuery-like chainable syntax and methods.
type Selection struct {
   
	Nodes    []*html.Node
	document *Document
	prevSel  *Selection
}

2.3文档操作函数:

Eq()
Index()
Last()
Slice()
Get()
······

二、采集代理IP:

1.代理IP池
  1. 高可用全球免费代理IP库
  2. 西刺免费代理IP
  3. 全网IP代理
  4. 快代理
2.goQuery采集:

不多说,上代码:

import "github.com/PuerkitoBio/goquery"

导入goquery库

//采集代理返回的参数
type proxyResult struct {
   
	Ip           string `json:"ip"`           //ip
	Port         int    `json:port`           //端口
	Agreement    string `json:agreement`      //请求协议
	Anonymous    string `json:anonymous`      //透明度
	Region       string `json:region`         //地区
	Speed        string `json:"speed"`        //响应速度
	Source       string `json:"source"`       //来源(采集资源站)
	Verification string `json:"verification"` //验证时间
}

//采集代理所需的参数
type proxyParamet struct {
   
	ipIndex           int `json:"ipIndex"`           //ip下标
	portIndex         int `json:"portIndex"`         //端口下标
	agreementIndex    int 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值