亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

數據抓取使用HTTP代理ip代碼示例

發布時間:2020-07-17 06:28:29 來源:網絡 閱讀:236 作者:qq5ceb91a5504fe 欄目:大數據

HTTP Proxy Demo 代碼

1、Python

#! -*- encoding:utf-8 -*-

import requests

# 要訪問的目標頁面
targetUrl = "http://ip.hahado.cn/ip"

# 代理服務器
proxyHost = "ip.hahado.cn"
proxyPort = "39010"

# 代理隧道驗證信息
proxyUser = "username"
proxyPass = "password"

proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
    "host" : proxyHost,
    "port" : proxyPort,
    "user" : proxyUser,
    "pass" : proxyPass,
}

proxies = {
    "http"  : proxyMeta,
    "https" : proxyMeta,
}

resp = requests.get(targetUrl, proxies=proxies)

print resp.status_code
print resp.text

2、C Sharp

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ip.hahado.cn/ip");

WebProxy myProxy = new WebProxy();

Uri newUri = new Uri("http://ip.hahado.cn:39010");

myProxy.Address = newUri;

myProxy.Credentials = new NetworkCredential("username", "password");

request.Proxy = myProxy;

3、PHP

// 要訪問的目標頁面
$targetUrl = "http://ip.hahado.cn/ip";
//$targetUrl = "http://ip.hahado.cn/switch-ip";
//$targetUrl = "http://ip.hahado.cn/current-ip";
// 代理服務器
define("PROXY_SERVER", "ip.hahado.cn:39010");
// 隧道身份信息
define("PROXY_USER", "username");
define("PROXY_PASS", "password");
$proxyAuth = base64_encode(PROXY_USER . ":" . PROXY_PASS);
$headers = implode("\r\n", [
    "Proxy-Authorization: Basic {$proxyAuth}",
    "Proxy-Switch-Ip: yes",
]);
$options = [
    "http" => [
        "proxy"  => $proxyServer,
        "header" => $headers,
        "method" => "GET",
    ],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

4、JAVA

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;

class ProxyAuthenticator extends Authenticator {
    private String user, password;

    public ProxyAuthenticator(String user, String password) {
        this.user     = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }
}

/**
 * 注意:下面代碼僅僅實現HTTP請求鏈接,每一次請求都是無狀態保留的,僅僅是這次請求是更換IP的,如果下次請求的IP地址會改變
 * 如果是多線程訪問的話,只要將下面的代碼嵌入到你自己的業務邏輯里面,那么每次都會用新的IP進行訪問,如果擔心IP有重復,
 * 自己可以維護IP的使用情況,并做校驗。
 */
public class ProxyDemo {
    public static void main(String args[]) throws Exception {
        // 要訪問的目標頁面
        String targetUrl = "http://ip.hahado.cn/ip";
        //String targetUrl = "http://ip.hahado.cn/switch-ip";
        //String targetUrl = "http://ip.hahado.cn/current-ip";

        // 代理服務器
        String proxyServer = "ip.hahado.cn";
        int proxyPort      = 39010;

        // 代理隧道驗證信息
        String proxyUser  = "username";
        String proxyPass  = "password";

        try {
            URL url = new URL(targetUrl);

            Authenticator.setDefault(new ProxyAuthenticator(proxyUser, proxyPass));

            // 創建代理服務器地址對象
            InetSocketAddress addr = new InetSocketAddress(proxyServer, proxyPort);
            // 創建HTTP類型代理對象
            Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);

            // 設置通過代理訪問目標頁面
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
            // 設置IP切換頭
            connection.setRequestProperty("Proxy-Switch-Ip","yes");

            // 解析返回數據
            byte[] response = readStream(connection.getInputStream());

            System.out.println(new String(response));
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    }

    /**
     * 將輸入流轉換成字符串
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;

        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();

        return outSteam.toByteArray();
    }
}

5、golang

package main

import (
    "net/url"
    "net/http"
    "bytes"
    "fmt"
    "io/ioutil"
)

const ProxyServer = "ip.hahado.cn:39010"

type ProxyAuth struct {
    License string
    SecretKey string
}

func (p ProxyAuth) ProxyClient() http.Client {
    proxyURL, _ := url.Parse("http://" + p.License + ":" + p.SecretKey + "@" + ProxyServer)
    return http.Client{Transport: &http.Transport{Proxy:http.ProxyURL(proxyURL)}}
}

func main()  {
    targetURI := "http://ip.hahaod.cn/ip"
    //targetURI := "http://ip.hahaod.cn/switch-ip"
    //targetURI := "http://ip.hahaod.cn/current-ip"

    // 初始化 proxy http client
    client := ProxyAuth{License: "username", SecretKey: "password"}.ProxyClient()

    request, _ := http.NewRequest("GET", targetURI, bytes.NewBuffer([] byte(``)))

    // 切換IP (只支持 HTTP)
    request.Header.Set("Proxy-Switch-Ip", "yes")

    response, err := client.Do(request)

    if err != nil {
        panic("failed to connect: " + err.Error())
    } else {
        bodyByte, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Println("讀取 Body 時出錯", err)
            return
        }
        response.Body.Close()

        body := string(bodyByte)

        fmt.Println("Response Status:", response.Status)
        fmt.Println("Response Header:", response.Header)
        fmt.Println("Response Body:\n", body)
    }
}
    更多代理設置教材請復制連接打開:https://v.duoip.cn/customer/signup/?sale=xujinyang1991
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

岱山县| 舟曲县| 河南省| 荆州市| 商都县| 玛多县| 铜山县| 丽江市| 徐汇区| 洛隆县| 曲周县| 丰城市| 郴州市| 云南省| 桐乡市| 德化县| 惠安县| 红原县| 湾仔区| 股票| 麦盖提县| 民勤县| 叙永县| 民权县| 建阳市| 栾城县| 聂荣县| 临高县| 石门县| 依安县| 大埔区| 樟树市| 施甸县| 扬中市| 民乐县| 莱芜市| 合作市| 沭阳县| 南岸区| 瑞丽市| 阳曲县|