package main

import (
"fmt"
"github.com/gocolly/colly"
"github.com/gocolly/colly/proxy"
"strconv"
"strings"
"log"
pTool "./mypack"
) type ProxyIp struct {
Ip string
Port int
IsHttps bool
UpdateTime int
SourceUrl string
TimeTolive int
AnonymousInfo string
Area string
InternetServiceProvider string
} var ProxyIpPool []ProxyIp func main() {
p := &ProxyIpPool
SourceUrl := "http://www.xicidaili.com/nt/"
// Instantiate default collector
c := colly.NewCollector(
// MaxDepth is 2, so only the links on the scraped page
// and links on those pages are visited
colly.MaxDepth(1),
colly.Async(true),
) // Limit the maximum parallelism to 1
// This is necessary if the goroutines are dynamically
// created to control the limit of simultaneous requests.
//
// Parallelism can be controlled also by spawning fixed
// number of go routines.
c.Limit(&colly.LimitRule{DomainGlob: "*", Parallelism: 12}) // On every a element which has href attribute call callback
c.OnHTML("tr", func(e *colly.HTMLElement) {
var item ProxyIp
e.ForEach("td", func(i int, element *colly.HTMLElement) {
t := element.Text
switch i {
case 1:
item.Ip = t
break
case 2:
p, n := strconv.Atoi(t)
if n == nil {
item.Port = p
}
break
case 3:
item.Area = t
break
case 4:
item.IsHttps = strings.Contains(strings.ToLower(t), "https")
break
default:
break
} })
item.SourceUrl = SourceUrl
*p = append(*p, item)
}) // Start scraping on https://en.wikipedia.org
c.Visit(SourceUrl)
// Wait until threads are finished
c.Wait() fmt.Println(*p)
fmt.Println("fmt.Println(*p)----------------------------------->") var a [] string
for _, v := range *p {
http := "http"
if v.IsHttps {
http = "https"
}
if v.Ip != "" && v.Port != 0 {
s := http + "://" + v.Ip + ":" + strconv.Itoa(v.Port)
fmt.Println(s)
a = append(a, s)
}
} fmt.Println("fmt.Println(*p)<-----------------------------------")
c = colly.NewCollector(
colly.AllowedDomains("cn.sonhoo.com"),
) rp, err := proxy.RoundRobinProxySwitcher(a...) if err != nil {
log.Fatal(err)
}
c.SetProxyFunc(rp) c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
c.Visit(e.Request.AbsoluteURL(link))
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
r.Headers.Set("User-Agent", pTool.RandomString())
})
c.Visit("http://cn.sonhoo.com/wukong/a58627") } 动态更新代理ip,实时使用

go colly proxy 代理ip 动态 ip的更多相关文章

  1. java中Proxy(代理与动态代理)

    转自: https://blog.csdn.net/pangqiandou/article/details/52964066 一.代理的概念 动态代理技术是整个java技术中最重要的一个技术,它是学习 ...

  2. 二、Linux 静态IP,动态IP配置

    Linux 静态IP,动态IP配置 第一步:激活网卡 系统装好后默认的网卡是eth0,用下面的命令将这块网卡激活. # ifconfig eth0 up 第二步:设置网卡进入系统时启动 想要每次开机就 ...

  3. 实体ip 虚拟ip 固定ip 动态ip

    实体 IP:在网络的世界里,为了要辨识每一部计算机的位置,因此有了计算机 IP 位址的定义.一个 IP 就好似一个门牌!例如,你要去微软的网站的话,就要去『 207.46.197.101 』这个 IP ...

  4. Linux 静态IP动态IP设置

    1.设置动态IP ifconfig eth0 192.168.1.12 设置后立即生效,重启机器后就无效了 2.设置静态IP 编辑文件 /etc/sysconfig/network-scripts/i ...

  5. Proxy 代理模式 动态代理 CGLIB

    代理的基本概念 几个英文单词: proxy [ˈprɒksi] n. 代理服务器:代表权:代理人,代替物:委托书: invoke [ɪnˈvəʊk] vt. 乞灵,祈求:提出或授引-以支持或证明:召鬼 ...

  6. Proxy 代理模式 动态代理 cglib MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. 设置Windows静态IP+动态IP

    静态IP 设置以太网属性 进入IPv4属性 设置IPv4 动态IP 同上方法,只不过选成了自动

  8. python + seleinum +phantomjs 设置headers和proxy代理

    python + seleinum +phantomjs 设置headers和proxy代理     最近因为工作需要使用selenium+phantomjs无头浏览器,其中遇到了一些坑,记录一下,尤 ...

  9. nodejs爬虫如何设置动态ip以及userAgent

    nodejs爬虫如何设置动态ip以及userAgent 转https://blog.csdn.net/u014374031/article/details/78833765 前言 在写nodejs爬虫 ...

随机推荐

  1. Codeforces 922F Divisibility (构造 + 数论)

    题目链接  Divisibility 题意 给定$n$和$k$,构造一个集合$\left\{1, 2, 3, ..., n \right\}$的子集,使得在这个集合中恰好有$k$对正整数$(x, y) ...

  2. codevs——1169 传纸条(棋盘DP)

    2008年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 小渊和小 ...

  3. PHP实现RabbitMQ的Publish/Subscribe

    <?php /** * Created by PhpStorm. * User: 豆腐居士 * Date: 2018/5/30 * Time: 上午11:01 */ class AqiTask ...

  4. 检查iOS app 是否升级为新版本

    之前我帮某公司做的一个iOS app,升级的时候发现闪退问题.后来检查是因为升级的时候数据库出现一点小问题导致对象为空. 下面这个代码可以检测程序是否更新了,从而进行相关处理: 1 2 3 4 5 6 ...

  5. 【java】安全加密MessageDigest的功能及用法【hash一致性算法】

    链接地址:https://blog.csdn.net/ma1kong/article/details/2662997 1.查看MessageDigest源码的注释说明 2.和hash一致性算法 什么关 ...

  6. 【POI】导出xls文件报错:The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

    使用POI导出xls文件,由于数据过多,导致导出xls报错如下: The maximum number of cell styles was exceeded. You can define up t ...

  7. dedecms调用新闻文章列表

    效果如下: 代码如下: <div class="list"> <ul class="d6 ico4"> {dede:list pages ...

  8. IIC设备驱动程序

    IIC设备是一种通过IIC总线连接的设备,由于其简单性,被广泛引用于电子系统中.在现代电子系统中,有很多的IIC设备需要进行相互之间通信 IIC总线是由PHILIPS公司开发的两线式串行总线,用于连接 ...

  9. MapWindowPoints

    中文名 MapWindowPoints Windows CE 1.0及以上版本 头文件 winuser.h 库文件 user32.lib MapWindowPoints函数把相对于一个窗口的坐标空间的 ...

  10. 清空catalina.out报错Permission denied

    今天在清空catalina.out的时候报错 [yangkun@sg logs]$ sudo echo '' > catalina.out -bash: catalina.out: Permis ...