GoLang的colly模块可以用来刮取网页,并根据HTML标签检索有用的信息。在本教程中,我们将学习如何使用GoLang刮取亚马逊产品数据。
用例
如果你正在围绕亚马逊产品建立一个系统,或者说你想定期跟踪一个产品的价格变化,那么刮取网页和收集有用的数据是有意义的。手动进行这项工作是不可能的,但我们可以写一个程序来下载网页,并根据HTML标签进行解析,获得信息。
使用GoLang Colly模块抓取亚马逊数据
第一步是将colly模块导入我们的Go程序中:
package main
import(
"fmt"
"github.com/gocolly/colly"
}
之后,在主方法中,我们必须初始化colly收集器:
c := colly.NewCollector(colly.AllowedDomains("www.amazon.in"))
我们必须调用Visit()方法来下载网页以便进一步处理:
c.Visit("https://www.amazon.in/s?k=keyboard")
在这里,我正在搜索 "键盘",然后我将解析页面上的所有产品,并在控制台打印它们的名称、评级和价格。
当我们调用Visit()函数时,首先会调用OnRequest()回调方法,在这里我们可以做一些预处理:
c.OnRequest(func(r *colly.Request){
fmt.Println("Link of the page:", r.URL)
})
最重要的回调函数是OnHTML(),我们将在这里处理收到的HTML响应。我们必须找出HTML标签,从中获取所需的信息。根据浏览器中收到的HTML,我可以看到搜索页面包含HTML标签 "div.s-result-list.s-search-results.sg-row",我们为每个项目设置了 "div.a-section.a-spacing-base"。以下标签包含我们在这个例子中所需要的信息:
- 名称 - span.a-size-base-plus.a-color-base.a-text-normal
- 明星 - span.a-icon-alt
- 价格 - span.a-price-whole
最终代码
package main
import(
"fmt" //formatted I/O
"github.com/gocolly/colly" //scraping framework
}
func main(){
c := colly.NewCollector(colly.AllowedDomains("www.amazon.in"))
c.OnRequest(func(r *colly.Request){
fmt.Println("Link of the page:", r.URL)
})
c.OnHTML("div.s-result-list.s-search-results.sg-row", func(h*colly.HTMLElement){
h.ForEach("div.a-section.a-spacing-base", func(_ int, h*colly.HTMLElement){
var name string
name = h.ChildText("span.a-size-base-plus.a-color-base.a-text-normal")
var stars string
stars = h.ChildText("span.a-icon-alt")
var price string
price = h.ChildText("span.a-price-whole")
fmt.Println("ProductName: ", name)
fmt.Println("Ratings: ", stars)
fmt.Println("Price: ", price)
})
})
c.Visit("https://www.amazon.in/s?k=keyboard")
}
从终端运行代码
首先,我们必须初始化go项目。我已经创建了一个新的目录,并将上述代码保存在 "scraper.go "文件中:
% go mod init scraper
go: creating new go.mod: module scraper
go: to add module requirements and sums:
go mod tidy
%
它将创建运行代码所需的两个文件--go.mod和go.sum。
下一步是为我们的项目获取colly模块:
% go get github.com/gocolly/colly
go get: added github.com/PuerkitoBio/goquery v1.8.0
go get: added github.com/andybalholm/cascadia v1.3.1
go get: added github.com/antchfx/htmlquery v1.2.5
go get: added github.com/antchfx/xmlquery v1.3.12
go get: added github.com/antchfx/xpath v1.2.1
go get: added github.com/gobwas/glob v0.2.3
go get: added github.com/gocolly/colly v1.2.0
go get: added github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
go get: added github.com/golang/protobuf v1.3.1
go get: added github.com/kennygrant/sanitize v1.2.4
go get: added github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca
go get: added github.com/temoto/robotstxt v1.1.2
go get: added golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b
go get: added golang.org/x/text v0.3.7
go get: added google.golang.org/appengine v1.6.7
%
现在,我们可以使用以下命令来运行我们的go程序:
% go run scraper.go
Link of the page: https://www.amazon.in/s?k=keyboard
ProductName: Logitech K380 Multi-Device Bluetooth Wireless Keyboard with Easy-Switch for Upto 3 Devices, Slim, 2 Year Battery for PC, Laptop, Windows, Mac, Chrome OS, Android, iPad OS, Apple TV (Dark Grey)
Ratings: 4.5 out of 5 stars
Price: 2,994
ProductName: HP 100 Wired Keyboard with USB Compatibility,Numeric keypad, Full Range of 109 Key(Including 12 Function Keys and 3 Hotkeys),Adjustable Height and Contoured Design.3-Years Warranty (2UN30AA)
Ratings: 4.3 out of 5 stars
Price: 449
ProductName: iClever BK10 Bluetooth Keyboard for Mac, Multi Device Wireless Keyboard Rechargeable Bluetooth 5.1 Stable Connection with Number Pad Ergonomic Design Keyboard for iPad, iPhone, Tablet, iOS, Android, Windows, Sliver/White
Ratings: 4.2 out of 5 stars
Price: 2,699
ProductName: HP 230 Wireless Black Keyboard with 2.4GHz connectivity up to 10m, 12 Function Keys and 16-Month Long Battery Life. 3-Years Warranty.(3L1E7AA)
Ratings: 4.1 out of 5 stars
Price: 1,299
ProductName: Logitech K580 Slim Multi-Device Wireless Keyboard – Bluetooth/Receiver, Compact, Easy Switch, 24 Month Battery, Win/Mac, Desktop, Tablet, Smartphone, Laptop Compatible - Graphite
Ratings: 4.5 out of 5 stars
Price: 3,495
ProductName: Zebronics ZEB-KM2100 Multimedia USB Keyboard Comes with 114 Keys Including 12 Dedicated Multimedia Keys & with Rupee Key
Ratings: 3.6 out of 5 stars
Price: 329
ProductName: Dell Km117 Wireless Keyboard Mouse-
Ratings: 4.2 out of 5 stars
Price:
%
接下来是什么?
- 以上是一个非常基本的程序,你可以看到有些项目没有找到价格,我们可以添加一些验证器来删除没有价格信息的项目。
- 我们可以进一步扩展这段代码,将信息保存到一些CSV文件或数据库中,以跟踪产品的价格变化。
- 最大的问题是,当响应中的HTML标签被改变时,它将破坏我们的程序。因此,我们可以从一些属性文件中读取标签,这些文件可以在不改变代码的情况下即时改变。
结论
网络刮削是一种非常普遍的方式,可以定期从网页上获取有用的信息。GoLang colly模块是创建这些程序的一个很好的选择,你可以使用专用代理来建立一个系统,以避免请求阻塞的问题。