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. bzoj 2889: Tree Conundrum

    2889: Tree Conundrum Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 37[Submit][Status][ ...

  2. 【spring cloud】子模块启动报错com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect

    spring cloud子模块启动报错 Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanic ...

  3. Swift 函数的定义与调用(Defining and Calling Functions)

    当你定义一个函数时,你能够定义一个或多个有名字和类型的值.作为函数的输入(称为參数.parameters).也能够定义某种类型的值作为函数运行结束的输出(称为返回类型). 每一个函数有个函数名,用来描 ...

  4. 对数据进行GZIP压缩和解压

    public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...

  5. 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之功能与数据分析

    一.引言 在学习了Nodejs和HTML5之后,发现了Nodejs的使用很方便,和php是完全不同的另一种后台语言.我也明白了,在一个项目里,是不可能同时存在Apach服务器(php)和Web服务器( ...

  6. IntelliJ IDEA 识别一个类所属的jar包package

    IntelliJ IDEA 识别一个类所属的jar包package 按住ctrl,鼠标移动上去,不要点击: 有木有快捷键? ctrl+alt+B直接就过去了:需要再跳回来:

  7. JNI开发(2)——开发实战

    JNI开发(1)--概述.环境搭建.必要知识点 JNI开发(2)--开发实战 本篇是重头戏:JNI实战开发.假设你对于 JNI.NDK 还没概念的话 那么观看本篇 也是没有太大难度的 ,哈哈哈哈! ! ...

  8. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览 client对象模型(CSOM)基础

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览  client对象模型(CSOM)基础         在SP2 ...

  9. json lib 2.4及其依赖包下载

    下载文件地址:https://files.cnblogs.com/files/xiandedanteng/json-lib-2.4%26dependencies_jars.rar 它包括 common ...

  10. vue install 注册组件

    1.myPlugin.js文件 let MyPlugin = {}; MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或属性 Vue. ...