package main

import (
"fmt"
"strconv"
"net/http"
"os"
"io"
) //百度贴吧的地址规律
//第一页:https://tieba.baidu.com/f?kw=%E7%BB%9D%E5%9C%B0%E6%B1%82%E7%94%9F&ie=utf-8(&pn=0)
//第二页:https://tieba.baidu.com/f?kw=%E7%BB%9D%E5%9C%B0%E6%B1%82%E7%94%9F&ie=utf-8&pn=50
//第三页:https://tieba.baidu.com/f?kw=%E7%BB%9D%E5%9C%B0%E6%B1%82%E7%94%9F&ie=utf-8&pn=100
//所以它的最后的数字每加50,代表着下一页 //整体提取的思路:
//1、先拿地址
//2、爬
//3、取
//4、存 func HttpGet(url string) (result string, err error) {
resp, err1 := http.Get(url)
if err != nil {
err = err1
return
}
defer resp.Body.Close()
//读取网页的body内容
buf := make([]byte, *)
for true {
n, err := resp.Body.Read(buf)
if err != nil {
if err == io.EOF{
fmt.Println("文件读取完毕")
break
}else {
fmt.Println("resp.Body.Read err = ", err)
break
}
}
result += string(buf[:n])
}
return
} func DoWork(start, end int) {
fmt.Printf("正在爬取第%d页到%d页\n", start, end)
for i:=start; i<=end; i++ {
url := "https://tieba.baidu.com/f?kw=%E7%BB%9D%E5%9C%B0%E6%B1%82%E7%94%9F&ie=utf-8&pn=" + strconv.Itoa((i-)*)
//爬,将所有的网页内容爬取下来
result, err := HttpGet(url)
if err != nil {
fmt.Println("http.Get err = ", err)
continue
}
//把内容写入到文件
filename := strconv.Itoa((i-)*) + ".html"
f, err1 := os.Create(filename)
if err1 != nil{
fmt.Println("os.Create err = ", err1)
continue
}
//写内容
f.WriteString(result)
//关闭文件
f.Close()
}
} func main() {
var start, end int
fmt.Printf("请输入起始页>=1:> ")
fmt.Scan(&start)
fmt.Printf("请输入结束页:> ")
fmt.Scan(&end)
DoWork(start, end)
}

[GO]百度贴吧的爬虫的更多相关文章

  1. 百度知道的php爬虫

    原文地址:百度知道的php爬虫作者:好宏杰软件 <?php class spider  {    private $content ;    private $contentlen ;    p ...

  2. “希希敬敬对”团队--‘百度贴吧小爬虫’Alpha版本展示博客

    希希敬敬对的 Alpha阶段测试报告 随笔链接地址 https://www.cnblogs.com/xiaoyoushang/p/10078826.html   Alpha版本发布说明 随笔链接地址 ...

  3. 百度图片爬虫-python版-如何爬取百度图片?

    上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...

  4. 百度贴吧的网络爬虫(v0.4)源码及解析

    更新:感谢评论中朋友的提醒,百度贴吧现在已经改成utf-8编码了吧,需要把代码中的decode('gbk')改成decode('utf-8').   百度贴吧的爬虫制作和糗百的爬虫制作原理基本相同,都 ...

  5. 百度翻译爬虫-Web版(自动生成sign)

    # 面向对象 # 百度翻译 -- 网页版(自动获取token,sign) import requests import js2py import json import re class WebFan ...

  6. [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析

    转自:http://blog.csdn.net/pleasecallmewhy/article/details/8934726 百度贴吧的爬虫制作和糗百的爬虫制作原理基本相同,都是通过查看源码扣出关键 ...

  7. Python爬虫教程-05-python爬虫实现百度翻译

    使用python爬虫实现百度翻译功能 python爬虫实现百度翻译: python解释器[模拟浏览器],发送[post请求],传入待[翻译的内容]作为参数,获取[百度翻译的结果] 通过开发者工具,获取 ...

  8. Python爬虫教程-实现百度翻译

    使用python爬虫实现百度翻译功能python爬虫实现百度翻译: python解释器[模拟浏览器],发送[post请求],传入待[翻译的内容]作为参数,获取[百度翻译的结果] 通过开发者工具,获取发 ...

  9. SuperSpider——打造功能强大的爬虫利器

    SuperSpider——打造功能强大的爬虫利器   1.爬虫的介绍 图1-1  爬虫(spider) 网络爬虫(web spider)是一个自动的通过网络抓取互联网 上的网页的程序,在当今互联网 中 ...

随机推荐

  1. git 怎么上传文件到github上

    1.安装git     sudo  apt-get install git 2.配置全局变量     git config --global user.name langhunm     git co ...

  2. sqlldr并发

    sage: SQLLDR keyword=value [,keyword=value,...]   部分关键字:     userid -- ORACLE username/password    c ...

  3. linux find中的-print0和xargs中-0的奥妙

    默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一行一行的: 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs ...

  4. Tkinter Dimensions

      Tkinter Dimensions: 各种长度,宽度,和其他部件的尺寸可以在许多不同的单位描述   各种长度,宽度,和其他部件的尺寸可以在许多不同的单位描述. 如果您设置了尺寸为整数,它被假定为 ...

  5. vue设置title和ioc图标

    vue设置ioc图标和title 1.ioc图标设置 在根目录中的index.html中引入代码: <link rel="shortcut icon" type=" ...

  6. C#中有关数组和string引用类型或值类型的判断

    直接来一段测试代码 class value_ref_type { public static void DEMO1() { ] { }; double[] location_new; string s ...

  7. 思科ASA 基础学习

    ASA int e0/0 ip add 192.168.1.1 24nameif insidesecruity-leve 100 int e0/0/0ip add 192.168.2.1 24name ...

  8. cmd使用代理

  9. 配置git 环境变量

    1.从Git官网下载windows版本的git:http://git-scm.com/downloads 2.一般使用默认设置即可:一路next,git安装完毕! 3.但是如果这时你打开windows ...

  10. eureka快速剔除失效服务

    eureka服务端配置 #eureka server刷新readCacheMap的时间,注意,client读取的是readCacheMap,这个时间决定了多久会把readWriteCacheMap的缓 ...