Golang Colly爬取图片gorm存储数据

发布于:2024-05-07 ⋅ 阅读:(23) ⋅ 点赞:(0)

语言:Golang

库:Iris/Colly/gorm

运行结果

text/html; charset=utf-8
It is image 20240429222029_0_0.jpg
Saved file: images\20240429222029_0_0.jpg
text/html; charset=utf-8
It is image 20240429222030_1_0.jpg
Saved file: images\20240429222030_1_0.jpg
It is image 20240429222031_1_1.jpg
Saved file: images\20240429222031_1_1.jpg
text/html; charset=utf-8
It is image 20240429222032_2_0.jpg
Saved file: images\20240429222032_2_0.jpg
text/html; charset=utf-8
It is image 20240429222033_3_0.jpg
Saved file: images\20240429222033_3_0.jpg
It is image 20240429222033_3_1.jpg
Saved file: images\20240429222033_3_1.jpg
It is image 20240429222035_3_2.jpg
Saved file: images\20240429222035_3_2.jpg
It is image 20240429222036_3_3.jpg
Saved file: images\20240429222036_3_3.jpg
It is image 20240429222036_3_4.jpg
Saved file: images\20240429222036_3_4.jpg
 

持久化数据

保存图片

相关代码

爬取方法:

func CrawlingDataFromXHS(requestUrl string) {
	c := colly.NewCollector()
	c.MaxDepth = 6

	detailC := c.Clone()

	// 主题编码
	var topicNo = 0
	// 图片编码
	var imageNo = 0

	c.OnRequest(func(r *colly.Request) {
		tools.AddHeadersForXHS(r)
		//_ = c.SetCookies(r.URL.Path, cookies)
	})

	// 下载指定标签 article 区域内所有的 img
	c.OnHTML("div.footer a.title", func(e *colly.HTMLElement) {
		// 存放主题编码
		topicNo = e.Index
		href := e.Attr("