golang 解析中国天气网的数据,goroutine 的方式问题

jerrychen · 2015-04-14 07:22:16 · 2096 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2015-04-14 07:22:16 的主题,其中的信息可能已经有所发展或是发生改变。

大家好,请教一下大家问题了;

ps:之前的代码有一点小问题,自己已经修改,现在这个版本ok!

下面的代码是最近学习golang,手痒就想做一个有一点使用的东西,而不是“hello world”; 然后就有了下面的代码;目前的代码可以执行的,

1:按现在的这种方式, 提示·2015/04/17 09:57:09 Get http://www.weather.com.cn/weather/101010600.shtml: EOF· 说明这个页面没有办法打开;然后就停止执行其他语句了,程序结束执行。 为什么呢?不是应该并行执行的吗?

2:对java比较数据,catch 后可以直接skip掉就ok了,不会影响其他的执行;golang的方式是什么?

package main

import (
    "fmt"
    "log"
    "runtime"
    // "strconv"
    "strings"
    "time"

    "github.com/PuerkitoBio/goquery"
)

type Weather struct {
    wind1   string
    temp1   string
    wea1    string
    wind    string
    temp2   string
    wea2    string
    wind3   string
    temp3   string
    wea3    string
    wind4   string
    temp4   string
    wea4    string
    wind5   string
    temp5   string
    wea5    string
    wind6   string
    temp6   string
    wea6    string
    zsindex map[string]string
}

func MaxParallelism() int {
    maxProcs := runtime.GOMAXPROCS(0)
    numCPU := runtime.NumCPU()
    if maxProcs < numCPU {
        return maxProcs
    }
    return numCPU
}

func ExampleScrape(url string, resultChan chan string) {
    //func ExampleScrape(url string) {
    doc, err := goquery.NewDocument("http://www.weather.com.cn/weather/" + url + ".shtml")
    if err != nil {
        log.Fatal(err)
    }

    defer func() {
        if err := recover(); err != nil {
            fmt.Println(err)
        }
    }()

    var listWeather [7][4]string
    var zsItemList [21]map[string]string
    doc.Find("#7d li").Each(func(i int, s *goquery.Selection) {
        var strWeather [4]string
        var zsItem = make(map[string]string)

        win1, _ := s.Find("p.win").First().Find("em span").First().Attr("title")
        win2, _ := s.Find("p.win").First().Find("em span").Last().Attr("title")
        temp1 := s.Find("p.tem.tem1").First().Text()
        temp2 := s.Find("p.tem.tem2").First().Text()
        weather := s.Find("p.wea").First().Text()
        strWeather[0] = temp2 + "~" + temp1
        strWeather[0] = strings.Replace(strWeather[0], "\n", "", -1)

        strWeather[1] = weather
        if win1 == win2 {
            strWeather[2] = win1
        } else {
            strWeather[2] = win1 + "转" + win2
        }
        strWeather[3] = s.Find("p.wea").First().Text()
        listWeather[i] = strWeather

        //

        docIndex, errIndex := goquery.NewDocument("http://www.weather.com.cn/weather1d/" + url + ".shtml")
        if errIndex != nil {
            log.Fatal(errIndex)
        }

        indexcount := 0
        docIndex.Find("div#zs0 li section.mask").Each(func(i int, s *goquery.Selection) {
            title, _ := s.Find("section.imgArea img").First().Attr("alt")
            content := s.Find("section.detail aside").First()
            index, _ := content.Find("b").First().Html()
            desc := content.Text()
            if len(strings.TrimSpace(index)) == 0 || len(strings.TrimSpace(desc)) == 0 {
                zsItem[title] = ""
            } else {
                zsItem[title] = index + "|" + desc
            }
            //fmt.Println(strconv.Itoa(i) + "|" + title + "|" + index + "|" + desc)
            zsItemList[i] = zsItem
            indexcount = i
        })

        docIndex.Find("div#zs1 li section.mask").Each(func(i int, s *goquery.Selection) {
            title, _ := s.Find("section.imgArea a").First().Attr("title")
            content := s.Find("section.detail aside[data-dn=today]").First()
            index, _ := content.Find("b").First().Html()
            desc := content.Text()

            if len(strings.TrimSpace(index)) == 0 || len(strings.TrimSpace(desc)) == 0 {
                zsItem[title] = ""
            } else {
                zsItem[title] = index + "|" + desc
            }
            //fmt.Println(strconv.Itoa(i) + "|" + title + "|" + index + "|" + desc)
            zsItemList[i+indexcount] = zsItem
        })
        //fmt.Println(url + "|" + listWeather[0][0] + "|" + listWeather[0][1] + "|" + listWeather[0][2] + "|" + listWeather[0][3] + "|" + zsItemList[1]["穿衣指数"])

    })
    resultChan <- url + "|" + listWeather[0][0] + "|" + listWeather[0][1] + "|" + listWeather[0][2] + "|" + listWeather[0][3] + "|" + zsItemList[1]["穿衣指数"]
}
func convertMaptoString(items map[string]string) string {

    result := ""
    for _, elem := range items {
        result += elem
    }
    return result

}

func main() {
    var urls = [...]string{
        "101010100",
        "101010200",
        "101010300",
        "101010400",
        "101010500",
        "101010600",
        "101010700",
        "101010800",
        "101010900",
        "101340406"}

    // t_start := time.Now()
    resultChan := make(chan string)
    for _, url := range urls {
        go ExampleScrape(url, resultChan)
    }

    go func() {
        for {
            select {
            case msg1 := <-resultChan:
                fmt.Println(msg1)
            case <-time.After(time.Second * 5):
                fmt.Println("timeout check again...")
            }
        }
    }()
    //ExampleScrape(urls[0])

    var input string
    fmt.Scanln(&input)
    // t_end := time.Now()
    // fmt.Println(t_end.Sub(t_start))

}

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2096 次点击  
加入收藏 微博
3 回复  |  直到 2015-04-18 07:52:15
xuanbao
xuanbao · #1 · 10年之前

recover 加上 if err := recover(); err != nil {} 判断

jerrychen
jerrychen · #2 · 10年之前
xuanbaoxuanbao #1 回复

recover 加上 `if err := recover(); err != nil {}` 判断

还是有error呀 ···101010600|13°C~28°C|雷阵雨转多云|北风|雷阵雨转多云|

2015/04/15 11:39:09 Get http://www.weather.com.cn/weather1d/101010100.shtml: EOF
exit status 1

exit status 1···
jerrychen
jerrychen · #3 · 10年之前
xuanbaoxuanbao #1 回复

recover 加上 `if err := recover(); err != nil {}` 判断

已经可以了,是因为其他地方有bug

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传