欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬取免費代理并驗證代理是否可用

 更新時間:2022年01月06日 16:10:52   作者:小軍遞  
這篇文章主要介紹了python爬取免費代理并驗證是否可用,通過本文給大家介紹了在什么情況下會用到代理并分享腳本的完整代碼,需要的朋友可以參考下

分享一個python腳本,使用代理ip來訪問網(wǎng)頁,方便抓取數(shù)據(jù)什么的~并自動驗證ip是否可以用

什么情況下會用到代理IP?比如你要抓取一個網(wǎng)站數(shù)據(jù),該網(wǎng)站有100萬條內(nèi)容,他們做了IP限制,每個IP每小時只能抓1000條,如果單個IP去抓因為受限,需要40天左右才能采集完,如果用了代理IP,不停的切換IP,就可以突破每小時1000條的頻率限制,從而提高效率。

腳本開始:

import requests
from lxml import etree
# 獲取快代理首頁的代理
def get_proxy_list():
    url = "https://www.jxmtjt.com/"
    payload = {}
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
        'Accept': 'application/json, text/javascript, */*; q=0.01',
    }
    response = requests.request("GET", url, headers=headers, data=payload)
    res = []
    _ = etree.HTML(response.text)
    type_dct = {
        "HTTP": "http://",
        "HTTPS": "https://"
    }
    data_list = _.xpath("http://tbody/tr")
    for data in data_list:
        ip = data.xpath("./td[1]/text()")[0]
        port = data.xpath("./td[2]/text()")[0]
        type = data.xpath("./td[4]/text()")[0]
        res.append(type_dct[type] + ip + ':' + port)
    return res
# 測試代理
def check(proxy):
    
    if 'https' in proxy:
        proxies = {'https': proxy}
    else:
        proxies = {'http': proxy}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4396.0 Safari/537.36'
    }
    try:
        r = requests.get(href, proxies=proxies, timeout=5, headers=headers)
        if r.status_code == 200:
            return True
    except:
        return False
if __name__ == '__main__':
    proxy_list = get_proxy_list()
    print(proxy_list)
    for p in proxy_list:
        print(p, check(p))
大家代碼復(fù)制后,把獲取代理ip的網(wǎng)址改一改就可以用了,代碼我一直在用,大家也可以cnblogs,百度搜索一下有沒有免費的代理ip獲取網(wǎng)址~

到此這篇關(guān)于python爬取免費代理并驗證是否可用的文章就介紹到這了,更多相關(guān)python爬取免費代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論