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. Linux:用tar解压文件出现错误Not found in archive

    问题:用tar解压文件出现错误Not found in archive 解决办法:加上-C参数  tar -zxvf ZenTaoPMS.8.1.3.zbox_64.gz -C /usr 因为压缩文件 ...

  2. docker启动容器报错 Unknown runtime specified nvidia.

    启动docker容器时,报错 问题复现 当我启动一个容器时,运行以下命令: docker run --runtime=nvidia .... 后面一部分命令没写出来,此时报错的信息如下: docker ...

  3. get、post请求方式在jmeter中使用步骤

    jmeter:性能测试工具,压测 一.jmeter工具测试接口时使用步骤: 1.测试计划右键--添加--Threads(Users)--线程组(线程数就是并发数) 2.线程组右键--Sampler-- ...

  4. python中实现参数化的原理

    k就是   <参数名>

  5. 运行netcore2.2程序是报错

    运行netcore2.2程序是报错 C:\myself\WuZhui\WuZhui\bin\Release\netcoreapp2.2>dotnet WuZhui.DLLError: An as ...

  6. JVM--先说本地方法接口

    本地方法接口 在讲Java虚拟机运行时数据区中本地方法栈之前,我们先来说说运行时数据区之外的一个叫本地方法接口的东西简称JNI(Java Native Interface) 简单来讲,一个Native ...

  7. 灵活使用 SQLAlchemy 中的 ORM 查询

    之前做查询一直觉得直接拼 SQL 比较方便,用了 SQLAlchemy 的 ORM 查询之后,发现也还可以,还提高了可读性. 这篇文章主要说说 SQLAlchemy 常用的 ORM 查询方式,偏实践. ...

  8. 7.kafka HA

  9. 【转】Locust性能-零基础入门系列(2) -重写wait_time

    在虚拟模拟的时候,可能对等待时间有更高的要求,比如假如有这么一个场景要求:某任务要求每被执行1次,那么下次的等待时间就1秒钟.这种情况,是可以实现的,这也就体现了Locust的灵活性.可编程性,很多比 ...

  10. Java12新特性

    switch表达式(预览) 传统switch的缺点 匹配是自上而下的,如果忘记写break, 后面的case语句不论匹配与否都会执行; 所有的case语句共用一个块范围,在不同的case语句定义的变量 ...