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. python sha256 键值对参数格式接口测试

    # coding=utf-8 import requests import time import uuid import hashlib import hmac import random impo ...

  2. Jenkins下Vue自动部署(二)

    1Jenkins配置 获取首次密码 sudo docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword 2 2.1J ...

  3. [LeetCode]394. 字符串解码(栈)

    题目 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数. ...

  4. js中数组Array对象的方法sort()的应用

    一. sort()方法的介绍 //给一组数据排序 var arrNum = [12,1,9,23,56,100,88,66]; console.log("排序前的数组:"+arrN ...

  5. pytest封神之路第四步 内置和自定义marker

    可以通过命令行查看所有marker,包括内置和自定义的 pytest --markers 内置marker 内置marker本文先讲usefixtures .filterwarnings .skip ...

  6. 请求转发和重定向实现与Ajax实现表单登陆

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException ...

  7. PHP数据库驱动扩展概述与不同方式连接数据库总结

    Author:极客小俊 一个专注于web技术的80后 我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人! CSDN@极客小俊,CSDN官方首发原创文章 个人博客: cnblogs.com ...

  8. Linux Shell脚本简单语法汇总(Deepin下运行)

    整理自: https://www.runoob.com/?s=shell Shell 脚本(shell script),是一种为 shell 编写的脚本程序. 业界所说的 shell 通常都是指 sh ...

  9. java经典算法40题-附带解决代码

    前一段时间工作比较闲,每天没有代码敲的日子有点无聊,于是为了保证自己的编程逻辑力的日常清醒,故百度了一些经典的java算法,然后自己思考编程解决问题,虽然那些东西比较基础了,但是有些题目小编看到了也是 ...

  10. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...