大家好,请教一下大家问题了;
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的方式是什么?
```go
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))
}
```
更多评论
还是有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···
#2