贴出代码:
```
package main
import (
"fmt"
"strconv"
"github.com/PuerkitoBio/goquery"
)
func PrintProxy(url string) {
g, e := goquery.NewDocument(url)
if e != nil {
fmt.Println(e)
}
g.Find("tr").Each(func(i int, s *goquery.Selection) {
if i > 0 {
node := s.Find("td")
IP := node.Eq(0)
Port := node.Eq(1)
fmt.Printf("%v:%v\n", IP.Text(), Port.Text())
}
})
}
func main() {
url := "http://www.kuaidaili.com/free/inha/"
for i := 1; i < 5; i++ {
u := url + strconv.Itoa(i)
fmt.Println(u)
PrintProxy(u)
fmt.Println("++++++")
}
}
```
打印:
```
http://www.kuaidaili.com/free/inha/1
112.111.217.74:808
117.90.2.140:9000
++++++
http://www.kuaidaili.com/free/inha/2
++++++
http://www.kuaidaili.com/free/inha/3
++++++
http://www.kuaidaili.com/free/inha/4
++++++
```
为何只调用了一次函数?
应该是有异步线程,没等返回主线程 函数退出了,在func PrintProxy里最后一行加time.Sleep(time.Second * 1)试试
#1
更多评论
package main
import (
"fmt"
"strconv"
"sync"
"github.com/PuerkitoBio/goquery"
)
var wg sync.WaitGroup
func PrintProxy(url string) {
g, e := goquery.NewDocument(url)
if e != nil {
fmt.Println(e)
}
g.Find("tr").Each(func(i int, s *goquery.Selection) {
if i > 0 {
node := s.Find("td")
IP := node.Eq(0)
Port := node.Eq(1)
fmt.Printf("%v:%v\n", IP.Text(), Port.Text())
}
})
wg.Done()
}
func main() {
url := "http://www.kuaidaili.com/free/inha/"
for i := 1; i < 5; i++ {
u := url + strconv.Itoa(i)
fmt.Println(u)
wg.Add(1)
PrintProxy(u)
fmt.Println("++++++")
}
wg.Wait()
}
#2
#1 楼 <a href="/user/gwll" title="@gwll">@gwll</a> 哈哈,搞了半天 发现是网站的限制,多次请求速度太快。
#3