1、爬捧腹网

网页规律:

https://www.pengfu.com/xiaohua_1.html   下一页 +1

https://www.pengfu.com/xiaohua_2.html

主页面规律:

<h1 class="dp-b"><a href="  一个段子url连接   “

段子url:

<h1>    标题  </h1>  只取1个

<div class="content-txt pt10"> 段子内容 <a id="prew" href=">

2、爬捧腹网

示例: 并发版本

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. )
  11.  
  12. func HttpGet(url string) (result string, err error) {
  13. resp, err1 := http.Get(url) //发送get请求
  14. if err1 != nil {
  15. err = err1
  16. return
  17. }
  18.  
  19. defer resp.Body.Close()
  20.  
  21. //读取网页内容
  22. buf := make([]byte, 4*1024)
  23. for {
  24. n, _ := resp.Body.Read(buf)
  25. if n == 0 {
  26. break
  27. }
  28.  
  29. result += string(buf[:n]) //累加读取的内容
  30. }
  31.  
  32. return
  33. }
  34.  
  35. //开始爬取每一个笑话,每一个段子 title, content, err := SpiderOneJoy(url)
  36. func SpiderOneJoy(url string) (title, content string, err error) {
  37. //开始爬取页面内容
  38. result, err1 := HttpGet(url)
  39. if err1 != nil {
  40. //fmt.Println("HttpGet err = ", err)
  41. err = err1
  42. return
  43. }
  44.  
  45. //取关键信息
  46. //取标题 <h1> 标题 </h1> 只取1个
  47. re1 := regexp.MustCompile(`<h1>(?s:(.*?))</h1>`)
  48. if re1 == nil {
  49. //fmt.Println("regexp.MustCompile err")
  50. err = fmt.Errorf("%s", "regexp.MustCompile err")
  51. return
  52. }
  53. //取内容
  54. tmpTitle := re1.FindAllStringSubmatch(result, 1) //最后一个参数为1,只过滤第一个
  55. for _, data := range tmpTitle {
  56. title = data[1]
  57. // title = strings.Replace(title, "\r", "", -1)
  58. // title = strings.Replace(title, "\n", "", -1)
  59. // title = strings.Replace(title, " ", "", -1)
  60. title = strings.Replace(title, "\t", "", -1)
  61. break
  62. }
  63.  
  64. //取内容 <div class="content-txt pt10"> 段子内容 <a id="prev" href="
  65. re2 := regexp.MustCompile(`<div class="content-txt pt10">(?s:(.*?))<a id="prev" href="`)
  66. if re2 == nil {
  67. //fmt.Println("regexp.MustCompile err")
  68. err = fmt.Errorf("%s", "regexp.MustCompile err2")
  69. return
  70. }
  71.  
  72. //取内容
  73. tmpContent := re2.FindAllStringSubmatch(result, -1)
  74. for _, data := range tmpContent {
  75. content = data[1]
  76. content = strings.Replace(content, "\t", "", -1)
  77. content = strings.Replace(content, "\n", "", -1)
  78. content = strings.Replace(content, "\r", "", -1)
  79. content = strings.Replace(content, "<br />", "", -1)
  80. break
  81. }
  82.  
  83. return
  84. }
  85.  
  86. //把内容写入到文件
  87. func StoreJoyToFile(i int, fileTitle, fileContent []string) {
  88. //新建文件
  89. f, err := os.Create(strconv.Itoa(i) + ".txt")
  90. if err != nil {
  91. fmt.Println("os.Create err = ", err)
  92. return
  93. }
  94.  
  95. defer f.Close()
  96.  
  97. //写内容
  98. n := len(fileTitle)
  99. for i := 0; i < n; i++ {
  100. //写标题
  101. f.WriteString(fileTitle[i] + "\n")
  102. //写内容
  103. f.WriteString(fileContent[i] + "\n")
  104.  
  105. f.WriteString("\n=================================================================\n")
  106. }
  107.  
  108. }
  109.  
  110. func SpiderPape(i int, page chan int) {
  111. //明确爬取的url
  112. //https://www.pengfu.com/xiaohua_1.html
  113. url := "https://www.pengfu.com/xiaohua_" + strconv.Itoa(i) + ".html"
  114. fmt.Printf("正在爬取第%d个网页:%s\n", i, url)
  115.  
  116. //开始爬取页面内容
  117. result, err := HttpGet(url)
  118. if err != nil {
  119. fmt.Println("HttpGet err = ", err)
  120. return
  121. }
  122.  
  123. //fmt.Println("r = ", result)
  124. //取,<h1 class="dp-b"><a href=" 一个段子url连接 "
  125. //解释表达式
  126. re := regexp.MustCompile(`<h1 class="dp-b"><a href="(?s:(.*?))"`)
  127. if re == nil {
  128. fmt.Println("regexp.MustCompile err")
  129. return
  130. }
  131.  
  132. //取关键信息
  133. joyUrls := re.FindAllStringSubmatch(result, -1)
  134. //fmt.Println("joyUrls = ", joyUrls)
  135.  
  136. fileTitle := make([]string, 0)
  137. fileContent := make([]string, 0)
  138.  
  139. //取网址
  140. //第一个返回下标,第二个返回内容
  141. for _, data := range joyUrls {
  142. //fmt.Println("url = ", data[1])
  143.  
  144. //开始爬取每一个笑话,每一个段子
  145. title, content, err := SpiderOneJoy(data[1])
  146. if err != nil {
  147. fmt.Println("SpiderOneJoy err = ", err)
  148. continue
  149. }
  150. //fmt.Printf("title = #%v#", title)
  151. //fmt.Printf("content = #%v#", content)
  152.  
  153. fileTitle = append(fileTitle, title) //追加内容
  154. fileContent = append(fileContent, content) //追加内容
  155. }
  156.  
  157. //fmt.Println("fileTitle= ", fileTitle)
  158. //fmt.Println("fileContent= ", fileContent)
  159.  
  160. //把内容写入到文件
  161. StoreJoyToFile(i, fileTitle, fileContent)
  162.  
  163. page <- i //写内容,写num
  164.  
  165. }
  166.  
  167. func DoWork(start, end int) {
  168. fmt.Printf("准备爬取第%d页到%d页的网址\n", start, end)
  169.  
  170. page := make(chan int)
  171.  
  172. for i := start; i <= end; i++ {
  173. //定义一个函数,爬主页面
  174. go SpiderPape(i, page)
  175. }
  176.  
  177. for i := start; i <= end; i++ {
  178. fmt.Printf("第%d个页面爬取完成\n", <-page)
  179. }
  180.  
  181. }
  182.  
  183. func main() {
  184. var start, end int
  185. fmt.Printf("请输入起始页( >= 1) :")
  186. fmt.Scan(&start)
  187. fmt.Printf("请输入终止页( >= 起始页) :")
  188. fmt.Scan(&end)
  189.  
  190. DoWork(start, end) //工作函数
  191. }

执行结果:

  1. D:\GoFiles\src\hello_01>go run get_pengfu.go
  2. 请输入起始页( >= 1) :1
  3. 请输入终止页( >= 起始页) :5
  4. 准备爬取第1页到5页的网址
  5. 正在爬取第5个网页:https://www.pengfu.com/xiaohua_5.html
  6. 正在爬取第2个网页:https://www.pengfu.com/xiaohua_2.html
  7. 正在爬取第3个网页:https://www.pengfu.com/xiaohua_3.html
  8. 正在爬取第4个网页:https://www.pengfu.com/xiaohua_4.html
  9. 正在爬取第1个网页:https://www.pengfu.com/xiaohua_1.html
  10. 4个页面爬取完成
  11. 3个页面爬取完成
  12. 1个页面爬取完成
  13. 5个页面爬取完成
  14. 2个页面爬取完成

  

Go语言之进阶篇爬捧腹网的更多相关文章

  1. Go语言之进阶篇爬百度贴吧并发版

    1.爬百度贴吧并发版 示例: package main import ( "fmt" "net/http" "os" "strco ...

  2. python3制作捧腹网段子页爬虫

    0x01 春节闲着没事(是有多闲),就写了个简单的程序,来爬点笑话看,顺带记录下写程序的过程.第一次接触爬虫是看了这么一个帖子,一个逗逼,爬取煎蛋网上妹子的照片,简直不要太方便.于是乎就自己照猫画虎, ...

  3. Android实战:手把手实现“捧腹网”APP(三)-----UI实现,逻辑实现

    Android实战:手把手实现"捧腹网"APP(一)-–捧腹网网页分析.数据获取 Android实战:手把手实现"捧腹网"APP(二)-–捧腹APP原型设计.实 ...

  4. Android实战:手把手实现“捧腹网”APP(二)-----捧腹APP原型设计、实现框架选取

    Android实战:手把手实现"捧腹网"APP(一)-–捧腹网网页分析.数据获取 Android实战:手把手实现"捧腹网"APP(二)-–捧腹APP原型设计.实 ...

  5. Android实战:手把手实现“捧腹网”APP(一)-----捧腹网网页分析、数据获取

    Android实战:手把手实现"捧腹网"APP(一)-–捧腹网网页分析.数据获取 Android实战:手把手实现"捧腹网"APP(二)-–捧腹APP原型设计.实 ...

  6. go语言之进阶篇接口转换

    1.go语音之进阶篇 示例: package main import "fmt" type Humaner interface { //子集 sayhi() } type Pers ...

  7. go语音之进阶篇爬百度贴吧单线程版本

    一.爬什么? 1.明确目标 : 知道你准备在那个范围或者网站去搜索 2.爬: 将所有的网站的内容全部爬下来 3.取:去掉对我们没用处的数据 4.处理数据:按照我们想要的方式存储或使用 二.百度贴吧小爬 ...

  8. go语言之进阶篇通过select实现斐波那契数列

    一.select作用 Go里面提供了一个关键字select,通过select可以监听channel上的数据流动. select的用法与switch语言非常类似,由select开始一个新的选择块,每个选 ...

  9. go语言之进阶篇创建goroutine协程

    1.goroutine是什么 goroutine是Go并行设计的核心.goroutine说到底其实就是协程,但是它比线程更小,十几个goroutine可能体现在底层就是五六个线程,Go语言内部帮你实现 ...

随机推荐

  1. 初探Runloop(一)

    iOS 的最大特点就是运行时. 保证运行时的就是RunLoop 1.什么是RunLoop呢? 从字面理解就是:运行循环 引用下官方文档的介绍: A run loop is an event proce ...

  2. php模板引擎之blade

    一.简介模板引擎 模板引擎是将网站的页面设计和PHP应用程序几乎完全分离的一种解决方案,它能让前端工程师专注页面搭建,让后台工程师专注功能实现,以便实现逻辑分离,让每个人发挥所长.模板引擎技术的核心是 ...

  3. 洛谷P1220关路灯[区间DP 提前计算代价]

    题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯. 为了给村 ...

  4. Codeforces.1040E.Network Safety(思路 并查集)

    题目链接 \(Description\) 有一张\(n\)个点\(m\)条边的无向图,每个点有点权.图是安全的当且仅当所有边的两个端点权值不同.保证初始时图是安全的. 现在有权值为\(x\)的病毒,若 ...

  5. Using Dtrace OEL 6.X

    http://www.hhutzler.de/blog/using-dtrace/ http://docs.oracle.com/cd/E37670_01/E38608/html/dt_sdtparg ...

  6. WinForm通用自动更新器AutoUpdater项目实战

    一.项目背景介绍 最近单位开发一个项目,其中需要用到自动升级功能.因为自动升级是一个比较常用的功能,可能会在很多程序中用到,于是,我就想写一个自动升级的组件,在应用程序中,只需要引用这个自动升级组件, ...

  7. 玩一下C#的语音识别

    在.NET4.0中,我可以借助System.Speech组件让电脑来识别我们的声音. 以上,当我说"name",显示"Darren",我说"age&q ...

  8. 迭代dict的value

    我们已经了解了dict对象本身就是可迭代对象,用 for 循环直接迭代 dict,可以每次拿到dict的一个key. 如果我们希望迭代 dict 对象的value,应该怎么做? dict 对象有一个 ...

  9. arcgis的afcore_libfnp.dll经常被360杀毒,删除,请到恢复区恢复

    arcgis的afcore_libfnp.dll经常被360杀毒,删除,请到恢复区恢复

  10. WebApp分析建模的工具

    最近Web工程课在学习分析建模工具的内容.这周作业就写我对WebApp建模工具的认识.Web建模工具有很多,但是专门为分析开发的却相对很少.下面介绍在进行分析时可以用的四类工具. UML工具.使用统一 ...