使用python3批量查询ip9000.txt的9000端口标题

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException

# 配置chromium浏览器选项
options = Options()
options.binary_location = "/usr/bin/chromium-browser"  # 指定chromium的路径
options.add_argument('--headless')  # 无头模式
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

# 初始化webdriver并指定chromedriver的位置
driver = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=options)

# 读取ip9000.txt文件中的IP地址
file_path = "ip9000.txt"
try:
    with open(file_path, 'r') as file:
        ip_addresses = file.readlines()

    # 去除每个IP地址的换行符并转换为所需的URL格式
    urls = [f"http://{ip.strip()}:9000" for ip in ip_addresses]

    # 访问每个URL并打印标题
    for url in urls:
        try:
            driver.get(url)
            title = driver.title
            if title:
                print(f"{url} - {title}")
        except (WebDriverException, Exception):
            # 忽略所有异常,不输出错误信息
            pass

finally:
    driver.quit()

# ip9000.txt文件中的IP地址格式,每行一个ip

你可能感兴趣的:(Linux,脚本集合,python,开发语言)