go - Golang Colly Scraping - 网站验证码抓住了我的刮擦
问题描述
我确实为亚马逊产品标题做了抓取,但亚马逊验证码抓住了我的抓取工具。我尝试了 10 次 - go run main.go(8 次抓住了我 - 2 次我刮掉了产品标题)
我对此进行了研究,但没有找到任何适用于 golang 的解决方案(只有 python)有什么适合我的解决方案吗?
package main
import (
"fmt"
"strings"0
"github.com/gocolly/colly"
)
func main() {
// Create a Collector specifically for Shopify
c := colly.NewCollector(
colly.AllowedDomains("www.amazon.com", "amazon.com"),
)
c.OnHTML("div", func(h *colly.HTMLElement) {
capctha := h.Text
title := h.ChildText("span#productTitle")
fmt.Println(strings.TrimSpace(title))
fmt.Println(strings.TrimSpace(capctha))
})
// Start the collector
c.Visit("https://www.amazon.com/Bluetooth-Over-Ear-Headphones-Foldable-Prolonged/dp/B07K5214NZ")
}
输出:
输入您在下面看到的字符对不起,我们只需要确保您不是机器人。为获得最佳效果,请确保您的浏览器接受 cookie。
解决方案
如果你不介意不同的包,我写了一个包来搜索 HTML(基本上是瘦包装器github.com/tdewolff/parse
):
package main
import (
"github.com/89z/parse/html"
"net/http"
"os"
)
func main() {
req, err := http.NewRequest(
"GET", "https://www.amazon.com/dp/B07K5214NZ", nil,
)
req.Header = http.Header{
"User-Agent": {"Mozilla"},
}
res, err := new(http.Transport).RoundTrip(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
lex := html.NewLexer(res.Body)
lex.NextAttr("id", "productTitle")
os.Stdout.Write(lex.Bytes())
}
结果:
Bluetooth Headphones Over-Ear, Zihnic Foldable Wireless and Wired Stereo
Headset Micro SD/TF, FM for Cell Phone,PC,Soft Earmuffs &Light Weight for
Prolonged Waring(Rose Gold)
推荐阅读
- python - 出现错误无法设置过滤器:libpcap 不可用。无法编译过滤器
- javascript - 发生错误时防止 iFRame 重新加载
- python - tkinter 中的 TODO 列表
- javascript - 如何引用列表函数的所有返回类型来组成 React hoc
- python - “画布”对象没有属性“_committed”
- python - 单行(从展平 DataFrame)到 Python 中的 DataFrame
- ruby - ruby on rails 的 API 问题:resultat?Message=Authorization+has+been+denied+for+this+request
- applescript - 如何识别已安装的外部卷?
- python - 在 AMQP 设备上回复 Hono 命令
- node.js - Paspport Github oauth 没有发回代币?