2016-08-29 1 views
0

何千ものhtmlファイルで何らかの発生を置き換える必要があります。私はこのためにLinuxスクリプトを使用するつもりです。 は、ここで私がしなければならない置き換えのいくつかの例ですからhtmlファイルで発生を置き換えます

<a class="wiki_link" href="/WebSphere+Application+Server">

へ:プレフィックスとして/合流/ディスプレイ/ WIKIHAB1を追加し、置き換え、意味<a class="wiki_link" href="/confluence/display/WIKIHAB1/WebSphere%20Application%20Server">

"+" "%20"で指定します。

私は、IMG、IFRAMEのように、他のタグのために同じことをやる、というように...

まず、どのツール私はそれを作るために使うべきでしょうか?セド?アーク?その他?

例があれば、本当にありがたいです。

ありがとう

答えて

0

若干の研究の後、美しいスープを見つけました。これは、htmlファイルを解析するためのPythonライブラリです。使用するのが簡単で、よく文書化されています。 私はPythonに関する経験がなく、問題なくコードを書くことができました。 ここでは、質問で言及した置換を行うためのPythonコードの例を示します。

#!/usr/bin/python 

import os 
from bs4 import BeautifulSoup 

#Replaces plus sign(+) by %20 and add /confluence... prefix to each 
#href parameter at anchor(a) tag that has wiki_link in class parameter 
def fixAnchorTags(soup): 
    tags = soup.find_all('a') 

    for tag in tags: 
     newhref = tag.get("href") 

     if newhref is not None: 
      if tag.get("class") is not None and "wiki_link" in tag.get("class"): 
       newhref = newhref.replace("+", "%20") 
       newhref = "/confluence/display/WIKIHAB1" + newhref 
       tag['href'] = newhref 

#Creates a folder to save the converted files     
def setup(): 
    if not os.path.exists("converted"): 
     os.makedirs("converted") 

#Run all methods for each html file in the current folder 
def run(): 
    for file in os.listdir("."): 
     if file.endswith(".html"): 
      print "Converting " + file 
      htmlfile = open(file, "r") 
      converted = open("converted/"+file, "w") 
      soup = BeautifulSoup(htmlfile, "html.parser") 

      fixAnchorTags(soup) 

      converted.write(soup.prettify("UTF-8")) 
      converted.close() 
      htmlfile.close() 

setup() 
run() 
関連する問題