go语言 goquery爬虫
goquery 类似ruby的gem nokogiri
goquery的选择器功能很强大,很好用。地址:https://github.com/PuerkitoBio/goquery
这是一个糗百首页的爬虫程序
package main import (
"fmt"
"log"
"net/http"
"strings" "github.com/PuerkitoBio/goquery"
) func qiubai_parse() {
res, err := http.Get("https://www.qiushibaike.com/hot/")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
} doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
} array := make([]map[string]string, 100)
doc.Find("#content-left .article ").Each(func(i int, s *goquery.Selection) {
hash := make(map[string]string)
url, _ := s.Find("a[class]").Attr("href")
hash["link"] = "https://www.qiushibaike.com" + url
sub_res, _ := http.Get(hash["link"])
sub_doc, _ := goquery.NewDocumentFromReader(sub_res.Body)
hash["all_content"] = sub_doc.Find(".content").Text()
like_num := s.Find(".likenum").Text()
hash["like_num"] = strings.Replace(like_num, " ", "", -1)
comment := s.Find(".main-text").Text()
hash["comment"] = strings.Replace(comment, like_num, "", -1)
fmt.Println(hash)
array = append(array, hash)
})
fmt.Println(array)
} func main() {
qiubai_parse()
}
go语言 goquery爬虫的更多相关文章
- 13. Go 语言网络爬虫
Go 语言网络爬虫 本章将完整地展示一个应用程序的设计.编写和简单试用的全过程,从而把前面讲到的所有 Go 知识贯穿起来.在这个过程中,加深对这些知识的记忆和理解,以及再次说明怎样把它们用到实处.由本 ...
- R语言网络爬虫学习 基于rvest包
R语言网络爬虫学习 基于rvest包 龙君蛋君:2015年3月26日 1.背景介绍: 前几天看到有人写了一篇用R爬虫的文章,感兴趣,于是自己学习了.好吧,其实我和那篇文章R语言爬虫初尝试-基于RVES ...
- Go语言实战爬虫项目
Go语言爬虫框架之Colly和Goquery Python爬虫框架比较多有requests.urllib, pyquery,scrapy等,解析库有BeautifulSoup.pyquery.Scra ...
- 【C/C++】用C语言编写爬虫—爬虫程序优化要点
写一个网络爬虫 用C语言来写一个网络爬虫,来获取一个网站上感兴趣的信息,抓取自己需要的一切. #include<cspider/spider.h>/* 自定义的解析函数,d为获取到的h ...
- dota玩家与英雄契合度的计算器,python语言scrapy爬虫的使用
首发:个人博客,更新&纠错&回复 演示地址在这里,代码在这里. 一个dota玩家与英雄契合度的计算器(查看效果),包括两部分代码: 1.python的scrapy爬虫,总体思路是pag ...
- R语言网页爬虫
R 是统计计算和数据分析的利器.给定一个数据集,利用前几章介绍到的 R 中灵活的数据结构或高性能计算,我们可以很方便地进行数据转换.建模和数值分析.一般来说,商业数据库会将数据以表格的形式很好地组织起 ...
- R 语言爬虫 之 cnblog博文爬取
Cnbolg Crawl a). 加载用到的R包 ##library packages needed in this case library(proto) library(gsubfn) ## Wa ...
- R语言rvest包网络爬虫
R语言网络爬虫初学者指南(使用rvest包) 钱亦欣 发表于 今年 06-04 14:50 5228 阅读 作者 SAURAV KAUSHIK 译者 钱亦欣 引言 网上的数据和信息无穷无尽,如 ...
- webmagic的设计机制及原理-如何开发一个Java爬虫
之前就有网友在博客里留言,觉得webmagic的实现比较有意思,想要借此研究一下爬虫.最近终于集中精力,花了三天时间,终于写完了这篇文章.之前垂直爬虫写了一年多,webmagic框架写了一个多月,这方 ...
随机推荐
- Springboot属性加载与覆盖优先级与SpringCloud Config Service配置
参考官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config. ...
- PostgreSQL中的表连接操作
- [转]windows环境下启动与停止.jar文件
原文地址:https://www.jianshu.com/p/b12fc379d171 1 .启动 在xx.jar同级目录下建立run.bat文件,在run.bat文件中编辑下列文本. @echo o ...
- python 把带小数的浮点型字符串转换为整数的解决方案
以下内容在python中完全可以接受: 将整数的字符串表示形式传递给 int 将float的字符串表示形式传递给 float 但是,如果你将float型的字符串传递给int将会得到错误. >&g ...
- ROS tf-增加坐标系
博客参考:https://www.ncnynl.com/archives/201702/1312.html ROS与C++入门教程-tf-增加坐标系 说明: 介绍如何为TF增加额外固定的坐标系 为何增 ...
- EF Core基本使用
Mysql: nuget 安装 Pomelo.EntityFrameworkCore.MySql Microsoft.EntityFrameworkCore.Design csprj 修改: < ...
- [Bayes] MCMC (Markov Chain Monte Carlo)
不错的文章:LDA-math-MCMC 和 Gibbs Sampling 可作为精进MCMC抽样方法的学习材料. 简单概率分布的模拟 Box-Muller变换原理详解 本质上来说,计算机只能生产符合均 ...
- LLBLGen update table with join
Table1 id Name 1 xxx 2 ooo Table2 Table1Id Table1Name Column1 Column2 Column3 1 sss xxxx xxxx xxxx 2 ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- JIT(just in time)即时编译器
JIT(just in time) 前端vs后端 在编译原理中,通常将编译分为前端和后端.其中前端会对程序进行词法分析.语法分析.语义分析,然后生成一个中间表达形式(称为IR:Intermediate ...