https://github.com/antchfx/htmlquery

package main

import (
"fmt"
"github.com/antchfx/htmlquery"
"log"
"net/http"
"time"
) func main() {
url := "http://quotes.toscrape.com/" req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3776.0 Safari/537.36")
client := &http.Client{Timeout: time.Second * 5}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close() doc,_ := htmlquery.Parse(resp.Body)
list := htmlquery.Find(doc, "//div[@class=\"quote\"]") for _,n := range list {
content := htmlquery.FindOne(n,".//span[1]")
author := htmlquery.FindOne(n,"/span[2]//small") fmt.Printf("%s-%s\n",htmlquery.InnerText(author), htmlquery.InnerText(content)) } }

结果

GOROOT=C:\Go #gosetup
GOPATH=E:\www\gopath #gosetup
C:\Go\bin\go.exe build -o C:\Users\Administrator\AppData\Local\Temp\___go_build_main_go.exe E:\www\go\main.go #gosetup
C:\Users\Administrator\AppData\Local\Temp\___go_build_main_go.exe #gosetup
Albert Einstein-“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”
J.K. Rowling-“It is our choices, Harry, that show what we truly are, far more than our abilities.”
Albert Einstein-“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”
Jane Austen-“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”
Marilyn Monroe-“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”
Albert Einstein-“Try not to become a man of success. Rather become a man of value.”
André Gide-“It is better to be hated for what you are than to be loved for what you are not.”
Thomas A. Edison-“I have not failed. I've just found 10,000 ways that won't work.”
Eleanor Roosevelt-“A woman is like a tea bag; you never know how strong it is until it's in hot water.”
Steve Martin-“A day without sunshine is like, you know, night.” Process finished with exit code 0

  

golang xpath解析网页的更多相关文章

  1. C# 使用XPath解析网页

    1.需要安装库HtmlAgilityPack ,官网http://htmlagilitypack.codeplex.com/ // From File var doc = new HtmlDocume ...

  2. 小程序开发-使用xpath解析网页html中的数据

    最新有个微信小程序的开发需求,需要从网页中提取一些元素信息,获取有效数据 1. 了解到微信小程序里面不能直接操作dom元素,所以我们需要使用一些其他的npm包 2. 经过查到各方面的文档,最新决定用x ...

  3. @1-4使用Xpath解析豆瓣短评

    使用Xpath解析豆瓣短评 Python爬虫(入门+进阶)     DC学院 本节课程主要介绍解析神器Xpath是什么.Xpath如何安装及使用,以及使用实际的例子讲解Xpath如何解析豆瓣短评的网页 ...

  4. 用Xpath选择器解析网页(lxml)

    在<爬虫基础以及一个简单的实例>一文中,我们使用了正则表达式来解析爬取的网页.但是正则表达式有些繁琐,使用起来不是那么方便.这次我们试一下用Xpath选择器来解析网页. 首先,什么是XPa ...

  5. python网络爬虫之解析网页的XPath(爬取Path职位信息)[三]

    目录 前言 XPath的使用方法 XPath爬取数据 后言 @(目录) 前言 本章同样是解析网页,不过使用的解析技术为XPath. 相对于之前的BeautifulSoup,我感觉还行,也是一个比较常用 ...

  6. python开发遇到的坑(1)xpath解析ValueError: Unicode strings with encoding declaration are not supported

    Traceback (most recent call last): File "/Users/*******.py", line 37, in <module> Bt ...

  7. Scrapy基础(六)————Scrapy爬取伯乐在线一通过css和xpath解析文章字段

    上次我们介绍了scrapy的安装和加入debug的main文件,这次重要介绍创建的爬虫的基本爬取有用信息 通过命令(这篇博文)创建了jobbole这个爬虫,并且生成了jobbole.py这个文件,又写 ...

  8. go语言解析网页利器goquery使用教程(爬虫必备)

    某些时候需要爬取网页中指定信息时,通常需要一些框架解析网页行成dom模型,然后来操作节点来获取相应的信息.在java中很显然就是Jsoup,而在Golang里,应该就是这个goquery了吧. goq ...

  9. python网络爬虫之解析网页的BeautifulSoup(爬取电影图片)[三]

    目录 前言 一.BeautifulSoup的基本语法 二.爬取网页图片 扩展学习 后记 前言 本章同样是解析一个网页的结构信息 在上章内容中(python网络爬虫之解析网页的正则表达式(爬取4k动漫图 ...

随机推荐

  1. C# 读取 ttf字体文件里的 Unicode

    因为爬虫要解析 &#x880cc这种字体编码的值,下载到一个ttf文件,用百度字体编辑器 打开,可以看到每个字符对应的Unicode (数字下方 $23.$2A...这些), 我需要拿到这些映 ...

  2. unzip命令笔记

    unzip命令 文件压缩与解压 unzip命令用于解压缩由zip命令压缩的".zip"压缩包. 语法 unzip(选项)(参数) 选项 -c:将解压缩的结果显示到屏幕上,并对字符做 ...

  3. oracle之三rman 备份

    rman 备份 7.1 归档方式下rman备份常用语法: 7.1.1 backup 备份 1)备份全库:1.1 RMAN> backup database format='/u01/myrman ...

  4. wsl 修改默认安装路径

    如果已经装了,先删除 mklink /j C:\Users\XXXX\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rh ...

  5. [补题]求a[i]+a[j]+i-j的最大值,要求i<j

    题目 如题. 例: 输入: 5 11 6 5 18 12 输出: 29 题解 思路: 一直是按着(a[i]+a[j])+(i-j)想后序的思路,不应该限制住自己,应该多考虑拆的方法.正确思路是把a[i ...

  6. 内存管理初始化源码3:bootmem

    start_kernel ——> setup_arch ——> arch_mem_init ——> bootmem_init ——> init_bootmem_node: 此时 ...

  7. 阿里云服务器外网IP无法访问网站

    1.添加IIS时添加了127.0.0.1的IP监听导致无法访问外网IP 添加IP监听:netsh http add iplisten 127.0.0.1显示IP监听:netsh http show i ...

  8. DASH流媒体MPD中的segmentTemplate

    SegmentTemplate利用MPD中的属性代入公式计算可以得到相关通配符的数值,来提供给客户端进行相关地址解析.相较于segmentList,使用 SegmentTemplate 的方式,能够很 ...

  9. 关于java基础语法的学习笔记

    *java语言特点 1,简单易用 2,跨平台 拥有JVM虚拟机(运行程序) 3,面向对象 4,支持多线程*java核心机制 1,java虚拟机 JVM 2,垃圾回收机制*JDK和JRE JDK JRE ...

  10. Vue结合Django-Rest-Frameword结合实现登录认证(一)

    作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/2436173500265335 微信公众 ...