Go 如何在线查找软件包?
我正在尝试使用LiteIDE x22运行go程序,但我收到了消息Go 如何在线查找软件包?,go,web-scraping,scraper,Go,Web Scraping,Scraper,我正在尝试使用LiteIDE x22运行go程序,但我收到了消息 C:/Go/bin/go.exe build [C:/Users/admins/Desktop/desktp/worm_scraper-master] worm_scraper.go:11:2: cannot find package "github.com/codegangsta/cli" in any of: C:\Go\src\pkg\github.com\codegangsta\cli (from $GOROOT
C:/Go/bin/go.exe build [C:/Users/admins/Desktop/desktp/worm_scraper-master]
worm_scraper.go:11:2: cannot find package "github.com/codegangsta/cli" in any of:
C:\Go\src\pkg\github.com\codegangsta\cli (from $GOROOT)
C:\users\admins\gostuff\src\github.com\codegangsta\cli (from $GOPATH)
worm_scraper.go:12:2: cannot find package "github.com/puerkitobio/goquery" in any of:
C:\Go\src\pkg\github.com\puerkitobio\goquery (from $GOROOT)
C:\users\admins\gostuff\src\github.com\puerkitobio\goquery (from $GOPATH)
Error: process exited with code 1.
我想这意味着它是在我的硬盘上而不是在网上找的,对吗?(顺便说一句,我对编程一无所知,只是想尝试其他人编写的东西)
如何让它访问web?
这是完整的代码
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/codegangsta/cli"
"github.com/puerkitobio/goquery"
)
const (
MainSite = "https://parahumans.wordpress.com/"
TableOfContents = "https://parahumans.wordpress.com/table-of-contents/"
)
type Arc struct {
Identifier string
Title string
Chapters []Chapter
}
type Chapter struct {
Title string
Url string
Tags []string
Paragraphs []Paragraph
Retries int
DatePosted string
}
type Paragraph string
// Format the paragraph
func (p *Paragraph) Format() {
s := string(*p)
// Handle emphasis
s = strings.Replace(s, "<em>", "*", -1)
s = strings.Replace(s, "</em>", "*", -1)
s = strings.Replace(s, "<i>", "*", -1)
s = strings.Replace(s, "</i>", "*", -1)
// Handle bold
s = strings.Replace(s, "<strong>", "**", -1)
s = strings.Replace(s, "</strong>", "**", -1)
s = strings.Replace(s, "<b>", "**", -1)
s = strings.Replace(s, "</b>", "**", -1)
// Remove new lines
s = strings.Replace(s, "\n", "", -1)
// And random double spaces
s = strings.Replace(s, ". ", ". ", -1)
*p = Paragraph(s)
}
// Return the Arc that the given chapter belongs to
func (ch *Chapter) WhichArc(arcList []*Arc) (*Arc, error) {
for _, arc := range arcList {
if strings.Replace(ch.Title[:2], ".", "", -1) == arc.Identifier {
return arc, nil
}
}
return &Arc{}, errors.New("chapter '" + ch.Title + "' did not match any Arcs")
}
// Parse a chapter and return it
func (ch *Chapter) Parse(done chan bool) {
if ch.Retries > 3 {
panic("Chapter url '" + ch.Url + "' has timed out too many times")
}
// Get the chapter
if strings.HasPrefix(ch.Url, "http") == false {
// Make sure it begins with http so goquery can use it
ch.Url = "https://" + ch.Url
}
doc, err := goquery.NewDocument(ch.Url)
if err != nil {
// Try again
ch.Retries++
go ch.Parse(done)
return
}
// Set the new chapter title
ch.Title = doc.Find("h1.entry-title").Text()
// Set the tags
doc.Find(".entry-meta a[rel=tag]").Each(func(_ int, s *goquery.Selection) {
ch.Tags = append(ch.Tags, s.Text())
if len(ch.Tags) == 0 {
ch.Tags = append(ch.Tags, "NONE")
}
})
// Get the date it was posted
ch.DatePosted = doc.Find("time.entry-date").Text()
// Now we'll get all the paragraphs
doc.Find(".entry-content > p").Each(func(_ int, s *goquery.Selection) {
// Check for the previous/next links
if len(s.Find("a").Nodes) > 0 {
return
}
// Get the paragraph HTML
st, _ := s.Html()
para := Paragraph("")
// Get the actual paragraph
if val, exists := s.Attr("padding-left"); exists && val == "30px" {
// Check to see if the paragraph is special (indented) block
para = Paragraph(" " + st)
} else if val, exists := s.Attr("text-align"); exists && val == "center" {
// Otherwise check to see if it's a separator paragraph
para = Paragraph("----------")
} else {
// It's just a normal paragraph in this case
para = Paragraph(st)
}
// And add the paragraph to the chapter
para.Format()
ch.Paragraphs = append(ch.Paragraphs, para)
})
// Finally, let's signal a success
done <- true
}
// Return a slice of Arcs extracted from the table of contents
func ParseArcs(s string) []*Arc {
arcs := []*Arc{}
r, _ := regexp.Compile(`[0-9]+`)
for _, line := range strings.Split(s, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "Arc") {
arcs = append(arcs, &Arc{
Identifier: r.FindString(line),
Title: line,
})
} else if strings.HasPrefix(line, "Epilogue") {
arcs = append(arcs, &Arc{
Identifier: "E",
Title: line,
})
}
}
return arcs
}
func main() {
// Define the app
app := cli.NewApp()
app.Name = "Worm Scraper"
app.Usage = "A tool to let you get an updated EPUB copy of the serial web novel Worm, by Wildbow"
app.Version = "1.0"
app.Author = "Benjamin Harris"
// Define the application flags
app.Flags = []cli.Flag{
cli.BoolFlag{"pdf", "Save the book as a PDF instead of an EPUB, if possible"},
cli.BoolFlag{"with-link", "Include a link to the chapter online"},
cli.BoolFlag{"with-tags", "Include the tags each chapter was posted under"},
cli.BoolFlag{"with-date", "Include the date each chapter was posted"},
}
// The heart of the application
app.Action = func(context *cli.Context) {
// Starting the program
fmt.Println("Starting to scrape Worm")
// Get the list of arcs from the table of contents
fmt.Println("Gathering links from table of contents...")
contents, err := goquery.NewDocument(TableOfContents)
if err != nil {
panic("Failed to get the table of contents! " + err.Error())
}
// Parse the arcs
arcs := ParseArcs(contents.Find(".entry-content").Text())
// Now get the links for the arc chapters
contents.Find(".entry-content a:not([class*=share-icon])").Each(func(_ int, s *goquery.Selection) {
ch := Chapter{}
ch.Title = strings.Replace(strings.TrimSpace(s.Text()), "\n", "", -1)
ch.Url, _ = s.Attr("href")
if ch.Title == "" {
return
}
arc, _ := ch.WhichArc(arcs)
arc.Chapters = append(arc.Chapters, ch)
})
// Manually add missing chapter in Epilogue
c := Chapter{
Title: "E.2",
Url: "https://parahumans.wordpress.com/2013/11/05/teneral-e-2/",
}
a, _ := c.WhichArc(arcs)
a.Chapters = append(a.Chapters, c)
copy(a.Chapters[1+1:], a.Chapters[1:])
a.Chapters[1] = c
// Now start getting the chapters
chapters := 0
done := make(chan bool)
for _, arc := range arcs {
for i, _ := range arc.Chapters {
chapters++
go arc.Chapters[i].Parse(done)
}
}
fmt.Println("Starting to parse", chapters, "chapters")
fmt.Print("Finished: ")
totalChapters := chapters
for {
select {
case <-done:
chapters--
fmt.Print(totalChapters-chapters, ",")
}
if chapters == 0 {
// We're done with all the chapters
close(done)
fmt.Println()
break
}
}
// And let's write all this stuff to a file now
fmt.Println("Saving results to file...")
f, err := os.OpenFile("Worm.md", os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
panic(err)
}
defer f.Close()
// Define pagebreak
PageBreak := "\n\n"
// Write the cover
f.WriteString("# Worm\n\n")
f.WriteString("By Wildbow\n\n")
f.WriteString("Website: " + MainSite)
// Now loop through the Arcs
for _, arc := range arcs {
f.WriteString(PageBreak + "# " + arc.Title)
for _, chapter := range arc.Chapters {
f.WriteString("\n\n")
f.WriteString("## " + chapter.Title + "\n\n")
if context.Bool("with-tags") {
f.WriteString("**Tags:** " + strings.Join(chapter.Tags, ", ") + " ")
}
if context.Bool("with-date") {
f.WriteString("**Date:** " + chapter.DatePosted + " ")
}
if context.Bool("with-link") {
f.WriteString("**Link:** " + chapter.Url + " ")
}
f.WriteString("\n\n")
// Now save the chapter's paragraphs
for _, p := range chapter.Paragraphs {
f.WriteString(string(p) + "\n\n")
}
}
}
// Now let's try to convert the markdown file into an ebook format (epub, pdf)
fmt.Print("Attempting to convert Markdown file... ")
cmdText := []string{"-S", "Worm.md", "--epub-chapter-level", "2", "-o", "Worm.epub"}
if context.Bool("pdf") {
cmdText = []string{"Worm.md", "-o", "Worm.pdf"}
PageBreak = `<div style="page-break-after: always;"></div>`
}
cmd := exec.Command("pandoc", cmdText...)
err = cmd.Run()
if err != nil {
fmt.Println("Conversion failed! Make sure you've installed Pandoc (http://johnmacfarlane.net/pandoc/installing.html) if you want to convert the generated Markdown file to an ebook compatible format. In the meantime, we've left you the Markdown file.")
} else {
_ = os.Remove("Worm.md")
fmt.Println("Completed!")
}
}
// Run the application
app.Run(os.Args)
}
主程序包
进口(
“错误”
“fmt”
“操作系统”
“os/exec”
“regexp”
“字符串”
“github.com/codegangsta/cli”
“github.com/purkitobio/goquery”
)
常数(
主站点=”https://parahumans.wordpress.com/"
目录=”https://parahumans.wordpress.com/table-of-contents/"
)
类型弧结构{
标识符字符串
标题字符串
第[]章
}
类型章节结构{
标题字符串
Url字符串
标签[]字符串
第[]段
重试整数
日期过帐字符串
}
键入段落字符串
//段落格式
func(p*段落)格式(){
s:=字符串(*p)
//处理重点
s=字符串。替换(s,“,”*,-1)
s=字符串。替换(s,“,”*,-1)
s=字符串。替换(s,“,”*,-1)
s=字符串。替换(s,“,”*,-1)
//大胆处理
s=字符串。替换(s,“”、“**”、-1)
s=字符串。替换(s,“”、“**”、-1)
s=字符串。替换(s,“,”**,-1)
s=字符串。替换(s,“,”**,-1)
//删除新行
s=字符串。替换(s、“\n”、“1”)
//和随机双空间
s=字符串。替换(s、“.,”、-1)
*p=段落(s)
}
//返回给定章节所属的弧
func(ch*章)WhichArc(arcList[]*弧)(*弧,错误){
对于u,弧:=范围弧列表{
if strings.Replace(ch.Title[:2],“,”,-1)=弧标识符{
返回弧,零
}
}
return&Arc{},errors.New(“chapter'+ch.Title+“'不匹配任何弧”)
}
//解析一章并返回它
func(ch*章)解析(已完成){
如果通道重试次数>3{
惊慌失措(“章节url'+ch.url+“'超时太多次”)
}
//拿到那一章
if strings.HasPrefix(ch.Url,“http”)==false{
//确保它以http开头,以便goquery可以使用它
ch.Url=“https://”+ch.Url
}
doc,err:=goquery.NewDocument(ch.Url)
如果错误!=零{
//再试一次
重试++
转到ch.Parse(完成)
返回
}
//设置新的章节标题
ch.Title=doc.Find(“h1.entry Title”).Text()
//设置标签
doc.Find(“.entry meta a[rel=tag]”)。每个(func(u)int,s*goquery.Selection){
ch.Tags=append(ch.Tags,s.Text())
如果len(ch.Tags)==0{
ch.Tags=附加(ch.Tags,“无”)
}
})
//获取它的发布日期
ch.DatePosted=doc.Find(“time.entry date”).Text()
//现在我们将获得所有段落
文档查找(“.entry content>p”).Each(func(u-int,s*goquery.Selection){
//检查上一个/下一个链接
如果len(s.Find(“a”)节点>0{
返回
}
//获取段落HTML
st,u:=s.Html()
段落:=段落(“”)
//获取实际段落
如果val存在:=s.Attr(“左填充”);存在&&val==“30px”{
//检查段落是否为特殊(缩进)块
段落=段落(“+st)
}否则,如果val存在:=s.Attr(“文本对齐”);存在&&val==“中心”{
//否则,请检查它是否是分隔段落
段落=段落(“------------”)
}否则{
//在这种情况下,这只是一个普通的段落
段落=段落(st)
}
//并将该段添加到本章中
第8段格式()
章节段落=附加(章节段落,段落)
})
//最后,让我们发出成功的信号
完成go编译器不会直接从internet导入库,但它知道如何为您获取库。当您导入类似github.com/codegangsta/cli
的内容时,它不会在该URL上查找,而是在GOPATH/src文件夹中查找
go-get
命令可以在库的URL中为您获取库并将其下载到您的GOPATH
如果您已经设置了GOPATH(如果没有,请阅读),那么在运行代码之前,请运行命令go get library
,以便go工具为您下载。在您的示例中,您应该运行以下命令:
go get github.com/codegangsta/cli
go get github.com/puerkitobio/goquery
这将把库分别下载到GOPATH/src/github.com/codegangsta/cli
和GOPATH/src/github.com/purkitobio/goquery
。go编译器不会直接从internet导入库,但它知道如何为您获取它们
它不会在该URL上查找,而是在您的GOPATH/src文件夹中查找
go-get
命令可以在库的URL中为您获取库并将其下载到您的GOPATH
如果您已经设置了GOPATH(如果没有,请阅读),那么在运行代码之前,请运行命令go get library
,以便go工具为您下载。在您的示例中,您应该运行以下命令:
go get github.com/codegangsta/cli
go get github.com/puerkitobio/goquery
这将把库分别下载到
GOPATH/src/github.com/codegangsta/cli
和GOPATH/src/github.com/purkitobio/goquery
。go编译器不会直接从internet导入库,但它知道如何为您获取它们它不会在该URL上查找,而是在您的GOPATH/src文件夹中查找
go-get
命令可以在库的URL中为您获取库并将其下载到您的GOPATH
如果您已经设置了GOPATH(如果没有,请阅读),那么在运行代码之前,请运行go工具要执行的命令go get library