<listing id="fdftx"></listing>

        <address id="fdftx"></address>
        <address id="fdftx"><address id="fdftx"><nobr id="fdftx"></nobr></address></address>
        <address id="fdftx"><listing id="fdftx"><nobr id="fdftx"></nobr></listing></address>

        <address id="fdftx"><nobr id="fdftx"><meter id="fdftx"></meter></nobr></address><address id="fdftx"><th id="fdftx"><progress id="fdftx"></progress></th></address>

          篩選免費ip代理的步驟

          發布時間:2020-07-08 關注熱度:°C

            在搜索引擎上找到的免費ip代理網站,我們對其進行爬取之后,這些IP需要經過檢測。因為免費ip代理的可用率是比較低的,因為它具有開放性,所以要經過下面步驟才能將有用的IP篩選出來。

          免費ip代理的篩選驗證步驟

            1、ip代理驗證

            由于免費的ip代理很多都是無法使用,或是不穩定,或是時效短。所以驗證ip代理是否可用,就非常有必要。主要驗證原理:使用ip代理去訪問網頁,判斷是否能夠正常訪問。在此我選擇的網站是“站長之家”,這個網站可用直接返回你當前使用的ip以及ip所在地。這里需要注意的是訪問前可以設定連接超時的時間如果訪問時間超過一定時間,就直接跳過這個ip代理。建議是設定在2秒內,具體的可以看以下函數:

            def ip_test(ip_proxies):

            """

            驗證單個ip代理是否可用

            :param ip_proxies: 待驗證ip,例如:101.96.10.36:88

            :return:

            """

            url = "http://ip.chinaz.com/"

            headers = {

            "Host": "ip.chinaz.com",

            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0",

            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",

            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",

            "Accept-Encoding": "gzip, deflate",

            "Referer": "https://blog.csdn.net/Winterto1990/article/details/51220307",

            "Connection": "keep-alive",

            "Upgrade-Insecure-Requests": "1",

            "Cache-Control": "max-age=0",

            }

            proxies = {"http": "http://" + ip_proxies, } # 設置代理

            res = requests.get(url, headers=headers, proxies=proxies, timeout=1) # timeout為設定的相應時長,建議在2秒內

            # 解析網頁

            soup = BeautifulSoup(res.text, "html.parser")

            info_list = soup.find_all("p", {"class": "getlist pl10"})

            for info in info_list:

            is_local = info.get_text()

            print(info.get_text())

            return is_local.find("XXX.XXX.XXX.XXX") # 判斷是否為本地的地址

            2、批量驗證ip代理

            要批量的對ip代理進行驗證,實際上這就是調用上一步中驗證ip代理中的程序。具體程序如下:

            def ip_batch_inspection(read_path, save_path):

            """

            驗證多個ip代理是否可用

            :param read_path: ip代理文件路徑

            :param save_path: 驗證可用的ip代理保存路徑

            :return:

            """

            with open(read_path, "r") as fr:

            lines = fr.readlines()

            fr.close()

            count = 0

            file_name = read_path.split("/")

            print(file_name[-1] + "文件共有 " + str(len(lines)) + " 條數據")

            for line in lines:

            count += 1

            ip_proxies = line.replace(" ", "")

            try:

            is_local = ip_test(ip_proxies) # 如果是本地ip,返回值為大于0數值

            if is_local < 0:

            with open(save_path, "a") as fs:

            fs.write(ip_proxies + " ")

            except Exception as e:

            pass

            # print("ip不可用")

            print("驗證中......%.2f%%" % (count/len(lines)*100))

            print("驗證完畢")

            完成以上2步之后,我們就完成了免費ip代理的使用前準備了,可以放到ip代理池,等到之后需要時候再調用。

           

          版權聲明:本文為IP海(iphai.cn)原創作品,未經許可,禁止轉載!

          Copyright © www.skldkt.com. All Rights Reserved. IP海 版權所有.
          IP海僅提供中國內IP加速服務,無法跨境聯網,用戶應遵守《服務條款》內容,嚴禁用戶使用IP海從事任何違法犯罪行為。
          ICP備案鄂ICP備19030659號-3 公安備案鄂公網安備42100302000141號 計算機軟件著作權證計算機軟件著作權證 ICP/EDI許可證ICP/EDI許可證:鄂B2-20200106

          微信掃一掃咨詢
          国产福利第一页