获取最后一页的HTTP请求(使用Golang)

huangapple go评论84阅读模式
英文:

Get last page http request golang

问题

我正在进行类似这样的http请求

resp, err := http.Get("http://example.com/")

然后我获取header link

link := resp.Header.Get("link")

这给我一个类似这样的结果:

<page=3>; rel="next",<page=1>; rel="prev";<page=5>; rel="last"

问题

我该如何将其解析为更易读的方式?我特别想获取last页,但firstnext页也应该有用。

我尝试过使用Split正则表达式,但没有成功。

英文:

I am doing an http requestlike this one:

resp, err := http.Get(&quot;http://example.com/&quot;)

Then I am getting the header link:

link := resp.Header.Get(&quot;link&quot;)

Which gives me a result like this:

&lt;page=3&gt;; rel=&quot;next&quot;,&lt;page=1&gt;; rel=&quot;prev&quot;;&lt;page=5&gt;; rel=&quot;last&quot;

Question

How can I parse this into a more legible way? I specifically trying to get the last page but firstand nextpage should useful as well.

I tried with Splitsand Regular expressionswithout success.

答案1

得分: 1

以下是如何匹配页面编号的解决方案。

http://play.golang.org/p/kzurb38Fwx

text := `&lt;page=3&gt;; rel=&quot;next&quot;,&lt;page=1&gt;; rel=&quot;prev&quot;;&lt;page=2&gt;; rel=&quot;last&quot;`
re := regexp.MustCompile(`&lt;page=([0-9]+)&gt;; rel=&quot;next&quot;,&lt;page=([0-9]+)&gt;; rel=&quot;prev&quot;;&lt;page=([0-9]+)&gt;; rel=&quot;last&quot;`)
matches:= re.FindStringSubmatch(text)
if matches != nil {
	next := matches[1]
	prev := matches[2]
	last := matches[3]
	fmt.Printf(&quot;next = %s, prev = %s, last = %s\n&quot;, next, prev, last)
}

后续编辑:您可能还可以使用xml包以相同的结果,通过将输出解析为XML,但您需要稍微转换一下输出。

英文:

Here's a solution of how to match your page numbers.

http://play.golang.org/p/kzurb38Fwx

text := `&lt;page=3&gt;; rel=&quot;next&quot;,&lt;page=1&gt;; rel=&quot;prev&quot;;&lt;page=2&gt;; rel=&quot;last&quot;`
re := regexp.MustCompile(`&lt;page=([0-9]+)&gt;; rel=&quot;next&quot;,&lt;page=([0-9]+)&gt;; rel=&quot;prev&quot;;&lt;page=([0-9]+)&gt;; rel=&quot;last&quot;`)
matches:= re.FindStringSubmatch(text)
if matches != nil {
	next := matches[1]
	prev := matches[2]
	last := matches[3]
	fmt.Printf(&quot;next = %s, prev = %s, last = %s\n&quot;, next, prev, last)
}

Later Edit: you can probably also use the xml package to achieve the same result, by parsing that output as an XML, but you would need to transform your output a bit.

答案2

得分: 1

你确定这是输出的格式吗?看起来其中一个;应该是,

一个带有多个值的单个链接的格式应该是这样的(注意在“prev”后面的逗号):

<page=3>; rel="next",<page=1>; rel="prev",<page=5>; rel="last"

应该按照,拆分每个链接的顺序。对于每个链接,应该按照;拆分值或键值对,然后如果值匹配<(.*=.*)>,则丢弃尖括号并使用剩余的键和值。

英文:

Are you sure that is the format of the output? It looks like one of ; should be a ,.
A single Link http header with multiple values, should be of the format (notice the comma after "prev")

&lt;page=3&gt;; rel=&quot;next&quot;,&lt;page=1&gt;; rel=&quot;prev&quot;,&lt;page=5&gt;; rel=&quot;last&quot;

The order should be split on , for each link. Split each link on ; for values or key-value pairs, and then if they value matches &lt;(.*=.*)&gt;, discard the angle brackets and use the remaining key and value.

huangapple
  • 本文由 发表于 2016年4月15日 09:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/36636634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定