package main
import (
"fmt"
"github.com/gocolly/colly/v2"
"github.com/gocolly/colly/v2/queue"
"os"
"regexp"
)
func main() {
url := "http://www.doupoxs.com/doupocangqiong/"
c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"))
contentCollector := c.Clone()
strpath, _ := os.Getwd()
q, _ := queue.New(10, &queue.InMemoryQueueStorage{MaxSize: 10000})
c.OnHTML(".xsbox li a", func(html *colly.HTMLElement) {
q.AddURL(html.Request.AbsoluteURL(html.Attr("href")))
})
contentCollector.OnHTML("body", func(html *colly.HTMLElement) {
title := html.ChildText(".entry-tit")
content := title
html.ForEach(".m-post p", func(i int, item *colly.HTMLElement) {
if i == 0 && regexp.MustCompile(`第(.*?)章`).MatchString(item.Text) {
return
}
content += "\n" + item.Text
})
path := strpath + "/a/" + title + ".txt"
f, err := os.Create(path)
if err != nil {
fmt.Println(err)
}
fmt.Println("正在下载:", title)
f.WriteString(content)
defer f.Close()
})
c.Visit(url)
q.Run(contentCollector)
}
go 抓取小说测试代码
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 想要获取最新实时新闻资讯吗?Python带实现全网爬取新浪新闻重要的头条资讯,你只要运行一下代码,就能快捷地...