2.txt中失败的url再按照流程遍历一遍,成功的还是保存在1.txt文本中,失败的保存在3.txt文本中
时间: 2024-04-17 15:26:59 浏览: 172
你可以使用以下修改后的代码来实现你的需求:
```python
from concurrent.futures import ThreadPoolExecutor
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 打开URL文件
with open('url.txt', 'r') as file:
urls = file.read().splitlines()
# 定义特定的域名
specific_domains = ['4qo4li.com:9516/register?i_code=']
# 创建ChromeOptions对象
chrome_options = Options()
chrome_options.add_argument("--incognito") # 启用无痕模式
def process_url(url, success_file, failure_file):
# 创建浏览器实例
driver = webdriver.Chrome(options=chrome_options) # 使用Chrome浏览器,需要下载对应的驱动并设置到环境变量中
# 构建完整的URL
full_url = 'http://' + url + '/a/index.php/54545'
# 打开浏览器并访问URL
driver.get(full_url)
# 等待页面跳转完成
driver.implicitly_wait(10) # 设置隐式等待时间,单位为秒
# 获取当前页面的URL
current_url = driver.current_url
# 判断当前页面的URL是否包含特定域名
if any(domain in current_url for domain in specific_domains):
# 写入成功文件
with open(success_file, 'a') as file:
file.write(url + '\n')
else:
# 写入失败文件
with open(failure_file, 'a') as file:
file.write(url + '\n')
# 关闭浏览器
driver.quit()
# 创建线程池
with ThreadPoolExecutor(max_workers=10) as executor:
# 提交任务给线程池
futures = [executor.submit(process_url, url, '1.txt', '3.txt') for url in urls]
# 等待所有任务完成
for future in futures:
future.result()
```
这段代码在`process_url`函数中添加了两个新的参数:`success_file`和`failure_file`,用于指定成功和失败的URL保存的文件名。根据判断结果,成功的URL将会追加到`1.txt`文件中,失败的URL将会追加到`3.txt`文件中。
注意,`1.txt`和`3.txt`文件需要事先创建并确保可写权限。
希望这可以帮助到你!如果还有其他问题,请随时提问。
阅读全文