在Golang的go-colly中如何限制第三方URL访问?
在Golang的go-colly中如何限制第三方URL访问? 我需要爬取类似 abc.com 这样的域名,但在访问过程中会重定向到许多第三方网址,比如 facebook.com、google.com 等。
Go Colly 是否有类似 Scrapy LinkExtractor 规则的域名限制规则?
3 回复
colly.Collector 有一个 AllowedDomains 字段。尝试设置这个字段。
// Collector 提供用于抓取任务的爬虫实例
type Collector struct {
// UserAgent 是 HTTP 请求使用的 User-Agent 字符串
UserAgent string
// MaxDepth 限制访问 URL 的递归深度
// 设置为 0 表示无限递归(默认值)
MaxDepth int
// AllowedDomains 是域名白名单
// 留空则允许访问任何域名
AllowedDomains []string
// DisallowedDomains 是域名黑名单
更多关于在Golang的go-colly中如何限制第三方URL访问?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
此外,收集器中的 RedirectHandler 也可以使用
gocolly/colly/blob/master/colly.go#L105
Async bool
// ParseHTTPErrorResponse 允许解析状态码非2xx的HTTP响应
// 默认情况下,Colly仅解析成功的HTTP响应。将ParseHTTPErrorResponse
// 设置为true以启用此功能
ParseHTTPErrorResponse bool
// ID 是收集器的唯一标识符
ID uint32
// DetectCharset 可以为没有显式字符集声明的非utf8响应体启用字符编码检测
// 此功能使用 https://github.com/saintfish/chardet
DetectCharset bool
// RedirectHandler 允许控制重定向的处理方式
RedirectHandler func(req *http.Request, via []*http.Request) error
store storage.Storage
debugger debug.Debugger
robotsMap map[string]*robotstxt.RobotsData
htmlCallbacks []*htmlCallbackContainer
xmlCallbacks []*xmlCallbackContainer
requestCallbacks []RequestCallback
responseCallbacks []ResponseCallback
errorCallbacks []ErrorCallback
scrapedCallbacks []ScrapedCallback


