簡易ip池

from selenium import webdriver
from bs4 import BeautifulSoup
from lxml import etree
from fake_useragent import UserAgent
import random
import requests
UA = UserAgent()

class GetIP:
    def __init__(self):
        self.browser = webdriver.Chrome('d:\chromedriver.exe')
        self.url = 'http://www.xicidaili.com/nn'

    def get_html(self):
        self.browser.get(self.url)
        html = self.browser.page_source
        html_tree = etree.HTML(html)
        ip_list = []
        all_ip = html_tree.xpath('//*[@id="ip_list"]/tbody/tr/td[2]')
        port = html_tree.xpath('//*[@id="ip_list"]/tbody/tr/td[3]')
        for i in range(len(all_ip)):
            ip = all_ip[i].text + ":" + port[i].text
            ip_list.append(ip)
        return ip_list
    def check_up(self):
        ip_list = self.get_html()
        headers = {
            'UserAgent':UA.random
        }
        url = 'http://www.ip138.com/'
        for ip in ip_list:
            proxies = {
                "http": "http://" + ip,
                "https": "https://" + ip
            }
            try:
                html = requests.get(url,headers=headers,proxies=proxies)
                if html.status_code == 200:
                    print(ip)
                else:
                    return False
            except Exception as e:
                print(e)


if __name__ == '__main__':
    getIp = GetIP()
    getIp.check_up()

附上驗證代碼

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()

# 設(shè)置代理
chromeOptions.add_argument("--proxy-server=http://119.101.117.130:9999")
# 一定要注意,=兩邊不能有空格,不能是這樣--proxy-server = http://202.20.16.82:10152
browser = webdriver.Chrome(r"D:\chromedriver.exe",chrome_options = chromeOptions)

# 查看本機ip,查看代理是否起作用
browser.get("http://httpbin.org/ip")
print(browser.page_source)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容