批量下载搜狗词库

时间:2019-06-21 12:55来源:计算机教程
vnsc5858威尼斯城官网,使用go语言写的批量下载搜狗词库的程序,刚刚接触go,肯定有不完善的地方 // webfetch project webfetch.gopackage mainimport ( "io/ioutil" "net/http" "os" "regexp")func main() { //打开一

vnsc5858威尼斯城官网,使用go语言写的批量下载搜狗词库的程序,刚刚接触go,肯定有不完善的地方

// webfetch project webfetch.go
package main

import (
    "io/ioutil"
    "net/http"
    "os"
    "regexp"
)

func main() {

    //打开一个用于存放词典的文本文件
    file, _ := os.Create("words.txt")
    defer file.Close()

    curl := "http://pinyin.sogou.com/dict/search.php?word=ali"
    resp, _ := http.Get(curl)
    defer resp.Body.Close()
    robots, _ := ioutil.ReadAll(resp.Body)
    patternList := regexp.MustCompile("\\<dl\\s class\\=\"dictlist\"\\>([\\S\\s] ?)\\<\\/dl\\>")
    mathces := patternList.FindAllSubmatch(robots, -1)
    patternId := regexp.MustCompile("\\?id=(\\d ?)\"")
    var pos int64 = 0
    var tempPos int = 0
    for _, c := range mathces {
        subc := c[1]
        tempId := patternId.FindAllSubmatch(subc, -1)
        for _, w := range tempId {
            pos = int64(tempPos)
            link := "http://pinyin.sogou.com/dict/download_txt.php?id="   string(w[1])
            words, _ := http.Get(link)
            defer words.Body.Close()
            wordsContent, _ := ioutil.ReadAll(words.Body)
            tempPos, _ = file.WriteAt(wordsContent, pos)
        }

    }

}
//该片段来自于http://outofmemory.cn

编辑:计算机教程 本文来源:批量下载搜狗词库

关键词: