Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python更改html文档的css属性?_Python_Html_Css - Fatal编程技术网

如何使用python更改html文档的css属性?

如何使用python更改html文档的css属性?,python,html,css,Python,Html,Css,我有一个包含许多HTML文档的目录。其中大多数包含代码块 .org链接{ /*组织链接*/ 颜色:#b58900; 字体大小:粗体; 文字装饰:下划线; } 在标记内。我想写一个脚本,删除行文本装饰:下划线并将每个文件中的此块的颜色更改为#2aa198 使用python可以实现这一点吗?您可以使用正则表达式进行必要的替换,如下所示: import re test = """ .org-link { /* org-link */ color: #b5

我有一个包含许多HTML文档的目录。其中大多数包含代码块

.org链接{
/*组织链接*/
颜色:#b58900;
字体大小:粗体;
文字装饰:下划线;
}
标记内。我想写一个脚本,删除行
文本装饰:下划线
并将每个文件中的此块的颜色更改为
#2aa198


使用python可以实现这一点吗?

您可以使用正则表达式进行必要的替换,如下所示:

import re

test = """
      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
"""

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

print re.sub(r'(org-link\s+\{.*\})', fix, test, flags=re.S)
  .org-link {
    /* org-link */
    color:#777;
    font-weight: bold;
  }
import re
import glob

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

for html_file in glob.glob('*.html'):
    print html_file
    with open(html_file) as f_input:
        html = re.sub(r'(org-link\s+\{.*\})', fix, f_input.read(), flags=re.S)

    with open(html_file, 'w') as f_output:
        f_output.write(html)
这将把案文转换如下:

import re

test = """
      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
"""

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

print re.sub(r'(org-link\s+\{.*\})', fix, test, flags=re.S)
  .org-link {
    /* org-link */
    color:#777;
    font-weight: bold;
  }
import re
import glob

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

for html_file in glob.glob('*.html'):
    print html_file
    with open(html_file) as f_input:
        html = re.sub(r'(org-link\s+\{.*\})', fix, f_input.read(), flags=re.S)

    with open(html_file, 'w') as f_output:
        f_output.write(html)
它首先识别合适的
组织链接
块,然后首先替换颜色,然后删除任何
文本装饰
条目

然后可以对脚本进行扩展,以便对给定文件夹中的所有HTML文件执行此操作,如下所示:

import re

test = """
      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
"""

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

print re.sub(r'(org-link\s+\{.*\})', fix, test, flags=re.S)
  .org-link {
    /* org-link */
    color:#777;
    font-weight: bold;
  }
import re
import glob

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

for html_file in glob.glob('*.html'):
    print html_file
    with open(html_file) as f_input:
        html = re.sub(r'(org-link\s+\{.*\})', fix, f_input.read(), flags=re.S)

    with open(html_file, 'w') as f_output:
        f_output.write(html)

使用Python 2.7.9进行测试,您可以使用正则表达式进行必要的替换,如下所示:

import re

test = """
      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
"""

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

print re.sub(r'(org-link\s+\{.*\})', fix, test, flags=re.S)
  .org-link {
    /* org-link */
    color:#777;
    font-weight: bold;
  }
import re
import glob

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

for html_file in glob.glob('*.html'):
    print html_file
    with open(html_file) as f_input:
        html = re.sub(r'(org-link\s+\{.*\})', fix, f_input.read(), flags=re.S)

    with open(html_file, 'w') as f_output:
        f_output.write(html)
这将把案文转换如下:

import re

test = """
      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
"""

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

print re.sub(r'(org-link\s+\{.*\})', fix, test, flags=re.S)
  .org-link {
    /* org-link */
    color:#777;
    font-weight: bold;
  }
import re
import glob

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

for html_file in glob.glob('*.html'):
    print html_file
    with open(html_file) as f_input:
        html = re.sub(r'(org-link\s+\{.*\})', fix, f_input.read(), flags=re.S)

    with open(html_file, 'w') as f_output:
        f_output.write(html)
它首先识别合适的
组织链接
块,然后首先替换颜色,然后删除任何
文本装饰
条目

然后可以对脚本进行扩展,以便对给定文件夹中的所有HTML文件执行此操作,如下所示:

import re

test = """
      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
"""

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

print re.sub(r'(org-link\s+\{.*\})', fix, test, flags=re.S)
  .org-link {
    /* org-link */
    color:#777;
    font-weight: bold;
  }
import re
import glob

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

for html_file in glob.glob('*.html'):
    print html_file
    with open(html_file) as f_input:
        html = re.sub(r'(org-link\s+\{.*\})', fix, f_input.read(), flags=re.S)

    with open(html_file, 'w') as f_output:
        f_output.write(html)

使用Python 2.7.9进行测试

文件中是否只有一个
文本装饰
css属性?其他块中是否有多个。可能重复文件中是否只有一个
文本装饰
css属性?其他块中是否有多个。可能重复