How to get the page name in go?

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

How to get the page name in go?

问题

我有一个函数,应该获取页面名称并打印出来。例如,如果URL是http://localhost:8080/login.html,该函数应该打印出login.html

英文:

I have a function which should get the page name and print it, for example, if the URL is http://localhost:8080/login.html the function should print login.html

答案1

得分: 2

如果你只需要解析URL,你可以使用以下代码:

package main

import (
	"fmt"
	"net/url"
)

func main() {
	URL := "http://localhost:8080/login.html"

	name, err := getPageName(URL)

	if err != nil {
		panic(err)
	}

	fmt.Println(name)
}

func getPageName(URL string) (string, error) {
	u, err := url.Parse(URL)

	if err != nil {
		return "", err
	}

	return u.Path[1:], nil // 去掉开头的 /
}

如果你需要获取页面的HTML并从<head>中解析标题,你可以使用go-query

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/PuerkitoBio/goquery"
)

func main() {
	URL := "https://stackoverflow.com"

	res, err := http.Get(URL)

	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)
	}

	// 加载HTML文档
	doc, err := goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		log.Fatal(err)
	}

	title := doc.Find("title").Text()

	fmt.Println(title)
}
英文:

If you only need to parse the URL you can use below:

package main

import (
	&quot;fmt&quot;
	&quot;net/url&quot;
)

func main() {
	URL := &quot;http://localhost:8080/login.html&quot;

	name, err := getPageName(URL)

	if err != nil {
		panic(err)
	}

	fmt.Println(name)
}

func getPageName(URL string) (string, error) {
	u, err := url.Parse(URL)

	if err != nil {
		return &quot;&quot;, err
	}

	return u.Path[1:], nil // To remove initial /

}

If you need to get page's HTML and parse the title from &lt;head&gt; you can use go-query

package main

import (
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;net/http&quot;

	&quot;github.com/PuerkitoBio/goquery&quot;
)

func main() {
	URL := &quot;https://stackoverflow.com&quot;

	res, err := http.Get(URL)

	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		log.Fatalf(&quot;status code error: %d %s&quot;, res.StatusCode, res.Status)
	}

	// Load the HTML document
	doc, err := goquery.NewDocumentFromReader(res.Body)
	if err != nil {
		log.Fatal(err)
	}

	title := doc.Find(&quot;title&quot;).Text()

	fmt.Println(title)
}

答案2

得分: 0

你可以使用以下代码来获取页面名称:

func GetPageName(address string) (string, error) {
    u, err := url.Parse(address)
    if err != nil {
        return "", err
    }

    params := strings.Split(u.Path, "/")

    // 首页
    if len(params) == 0 || (len(params) == 1 && params[0] == "") {
        return "", nil
    } else {
        pageName := params[len(params)-1]
        // 有尾部斜杠
        if pageName == "" {
            return params[len(params)-2], nil
        }
        // 没有尾部斜杠
        return pageName, nil
    }
}

如果URL地址是首页地址,例如 host.comhost.com/,它将返回一个空字符串。
否则,它将返回给定URL的页面名称。例如,对于 host.com/testhost.com/test/host.com/path/test,它将返回 test

英文:

You can do this to get the page name:

func GetPageName(address string) (string, error) {
    u, err := url.Parse(address)
    if err != nil {
	    return &quot;&quot;, err
    }

    params := strings.Split(u.Path, &quot;/&quot;)

    // Index page
    if len(params) == 0 || (len(params) == 1 &amp;&amp; params[0] == &quot;&quot;) {
	    return &quot;&quot;, nil
    } else {
	    pageName := params[len(params)-1]
	    // Has trailing slash
	    if pageName == &quot;&quot; {
		    return params[len(params)-2], nil
	    }
	    // Doesn&#39;t have trailing slash
	    return pageName, nil
    }
}

If the url address is the index page address, for example host.com or host.com/ it returns an empty string.
Otherwise returns the page name for the given url. For example test for host.com/test and host.com/test/ and host.com/path/test.

huangapple
  • 本文由 发表于 2022年9月4日 23:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/73600634.html
匿名

发表评论

匿名网友

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

确定