php判断文本中是否包含指定文本
时间: 2023-10-02 17:06:55 浏览: 64
在PHP中,可以使用strpos函数来判断一个字符串中是否包含指定的文本。该函数会返回指定文本在字符串中第一次出现的位置,如果没有找到则返回false。可以根据返回值进行判断。
例如,判断一个字符串中是否包含指定的文本“hello”:
```php
$str = "world, hello!";
if (strpos($str, "hello") !== false) {
echo "包含指定文本";
} else {
echo "不包含指定文本";
}
```
上述代码中,strpos函数的返回值不等于false表示字符串中包含指定文本,否则不包含。需要注意的是,判断字符串中是否包含指定文本时,需要使用全等运算符(!==),因为strpos函数可能返回0,而0被视为false,使用全等运算符可以避免这种情况。
相关问题
再次处理failed_urls.txt中失败的URL 判断是否仍然失败,写入2.txt文件
要再次处理`failed_urls.txt`中的失败URL,并判断是否仍然失败,将仍然失败的URL写入`2.txt`文件中,可以进行如下修改:
```python
from concurrent.futures import ThreadPoolExecutor, wait
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):
# 创建浏览器实例
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):
# 写入1.txt文本文件
with open('1.txt', 'a') as file:
file.write(url + '\n')
else:
# 写入2.txt文本文件
with open('2.txt', 'a') as file:
file.write(url + '\n')
# 关闭浏览器
driver.quit()
# 创建线程池
with ThreadPoolExecutor(max_workers=10) as executor:
# 提交任务给线程池
futures = [executor.submit(process_url, url) for url in urls]
# 等待所有任务完成
wait(futures)
# 打开failed_urls.txt文件
with open('failed_urls.txt', 'r') as file:
failed_urls = file.read().splitlines()
# 创建新的线程池用于处理失败的URL
with ThreadPoolExecutor(max_workers=10) as executor:
# 提交任务给线程池
futures = [executor.submit(process_url, url) for url in failed_urls]
# 等待所有任务完成
wait(futures)
# 将仍然失败的URL写入2.txt文件中
for future, url in zip(futures, failed_urls):
if future.exception() is not None:
with open('2.txt', 'a') as file:
file.write(url + '\n')
```
在代码中,首先使用线程池处理原始的URL列表。然后,打开`failed_urls.txt`文件,读取其中的失败URL,并使用新的线程池处理这些URL。在处理完所有失败URL后,通过检查每个任务的异常状态来判断是否仍然失败,将仍然失败的URL写入`2.txt`文件中。
希望这个修改能满足您的需求。如果还有其他问题,请随时提问。
2.txt中失败的url再按照流程遍历一遍,成功的还是保存在1.txt文本中,失败的保存在3.txt文本中
你可以使用以下修改后的代码来实现你的需求:
```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`文件需要事先创建并确保可写权限。
希望这可以帮助到你!如果还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)