Мне нужно написать скрипт PowerShell, который изменит определенные теги в файле HTMLHtml

Программисты Html
Ответить Пред. темаСлед. тема
Anonymous
 Мне нужно написать скрипт PowerShell, который изменит определенные теги в файле HTML

Сообщение Anonymous »

Мне нужно написать скрипт PowerShell, который будет искать конкретный на основе тега, он является внутренним текстом, а затем изменяет значения внутреннего текста любых тегов

Код: Выделить всё

  под тем, что у меня есть   тег. 
ниже. Он ищет тег   с внутренним текстом «HTML COM объекта».  После того, как он обнаружил, он должен повторять через детские элементы этого   ищущий   теги и изменяя их внутренний текст со значением $ targeth1text. 
он обнаруживает теги  , но не находит теги  .# Specify the path to your HTML file
$htmlFilePath = "c:\myhtml.html"

# Specify the target H1 inner text
$targetH1Text = "html COM object"

# Specify the new inner text for code tags
$newCodeText = "Modified Code Content"

# Load the HTML document
$html = New-Object -ComObject "HTMLFile"
$html.IHTMLDocument2_write((Get-Content -Path $htmlFilePath -Raw))

# Find the H1 tag with the specified inner text
$targetH1 = $html.getElementsByTagName("h1") | Where-Object { $_.innerText -eq $targetH1Text }

# If the H1 tag is found, proceed to modify code tags within its descendants
if ($targetH1) {
# Find all code tags within the target H1's descendants
$codeTags = $targetH1.getElementsByTagName("code")
# Iterate through the found code tags and modify their inner text
foreach ($codeTag in $codeTags) {
$codeTag.innerText = $newCodeText
}

# Save the modified HTML back to the file
$html.IHTMLDocument2_close() # Close the document before saving changes
Set-Content -Path $htmlFilePath -Value $html.documentElement.outerHTML
Write-Host "Code tags under '$targetH1Text' H1 modified successfully."
} else {
Write-Host "H1 tag with inner text '$targetH1Text' not found."
}
< /code>
Ниже приведен мой HTML, который я поместил в файл с именем c: \ myhtml.html < /p>


This is html COM object example

Some text
Old Code 1
Another Header

Код: Выделить всё

Another Code Tag
html COM object
Content under matching h1

Код: Выделить всё

Code to Change
More text



Подробнее здесь: https://stackoverflow.com/questions/796 ... in-an-html
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Html»