duandao2306 2015-04-13 21:14
浏览 12
已采纳

传递标志变量去程序导致奇怪的输出

sergiotapia at Macbook-Air in ~/Work/go/src/github.com/sergiotapia/gophers on master [!]
$ go build && go install && gophers -github_url=https://github.com/search?utf8=%E2%9C%93&q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100&type=Users&ref=advsearch&l=
[1] 51873
[2] 51874
[3] 51875
[4] 51877
[2]   Done                    q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100
[3]   Done                    type=Users
[4]+  Done                    ref=advsearch

I'm trying to use the long github url as a parameter in my code for Gophers. It works fine for all other url types such as organisations or stargazers. However when I try to use the search results page I get the strange output above.

https://github.com/search?utf8=%E2%9C%93&q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100&type=Users&ref=advsearch&l=

package main

import (
    "flag"
    "log"
    "strings"

    "github.com/PuerkitoBio/goquery"
)

type user struct {
    name     string
    email    string
    url      string
    username string
}

func main() {
    url := flag.String("github_url", "", "github url you want to scrape")
    flag.Parse()
    githubURL := *url
    doc, err := goquery.NewDocument(githubURL)
    if err != nil {
        log.Fatal(err)
    }

    if strings.Contains(githubURL, "/orgs/") {
        scrapeOrganization(doc, githubURL)
    } else if strings.Contains(githubURL, "/search?") {
        scrapeSearch(doc, githubURL)
    } else if strings.Contains(githubURL, "/stargazers") {
        scrapeStarGazers(doc, githubURL)
    } else {
        scrapeProfile(doc)
    }
}
  • 写回答

1条回答 默认 最新

  • doukaizha5417 2015-04-13 21:18
    关注

    It's a bash command line (or whatever the mac uses). & and ? are shell metacharacters that you MUST escape. The shell has absolutely no idea what a URL is, nor should it ever have to.

    go 'http://....'
       ^-----------^
    

    Adding quotes will prevent the shell from parsing the metacharacters. The alternative is to manually escape each and ever metachar yourself:

    go http://example.com/script.php\?foo=bar\&baz=qux
                                    ^--------^
    

    which quickly gets tedious, and error prone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog