0

「.nmv-fas」のすべてのインスタンスを「title」タグの間にあるものに変更するにはどうすればよいですか?これはPythonで可能ですか、それとももっと良い方法がありますか?

基本的に変更:

これ

<html>
<head>
<title>.rtpv05-tl</title>
</head>
<a href="http://www.youversion.com/bible/gen.1.nmv-fas">http://www.youversion.com/bible/gen.1.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.2.nmv-fas">http://www.youversion.com/bible/gen.2.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.3.nmv-fas">http://www.youversion.com/bible/gen.3.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.4.nmv-fas">http://www.youversion.com/bible/gen.4.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.5.nmv-fas">http://www.youversion.com/bible/gen.5.nmv-fas</a>

これに

<html>
<head>
<title>.rtpv05-tl</title>
</head>
<a href="http://www.youversion.com/bible/gen.1.rtpv05-tl">http://www.youversion.com/bible/gen.1.rtpv05-tl</a>
<a href="http://www.youversion.com/bible/gen.2.rtpv05-tl">http://www.youversion.com/bible/gen.2.rtpv05-tl</a>
<a href="http://www.youversion.com/bible/gen.3.rtpv05-tl">http://www.youversion.com/bible/gen.3.rtpv05-tl</a>
<a href="http://www.youversion.com/bible/gen.4.rtpv05-tl">http://www.youversion.com/bible/gen.4.rtpv05-tl</a>
<a href="http://www.youversion.com/bible/gen.5.rtpv05-tl">http://www.youversion.com/bible/gen.5.rtpv05-tl</a>
4

2 に答える 2

1
awk -v text='.nmv-fas' '
    /<title>/ {title=$0; gsub(/<\/?title>/, "", title); replace=1}
    replace {gsub(text, title)}
    {print}
' file > file.tmp && mv file.tmp file

awk には、sed のような「インプレース」オプションがありません-i

もちろん、これはタイトル テキストが<title>タグと同じ行にあることに依存します。安全のために、HTML パーサーを使用して HTML を解析する必要があります。

于 2012-05-10T18:53:32.463 に答える
0

正規表現を使用して、タイトルを文字列として引き出すことができます。あなたのhtmlがいくつかの文字列にあると仮定します:

import re
match = re.compile(r"<title>(.+)</title>",re.I|re.DOTALL)
title = match.group(1)

次に、文字列に対して文字列置換を実行します。

s.replace(".nmv-fas",title)
于 2012-05-10T18:57:49.810 に答える