【问题标题】:Renaming HTML files using <title> tags使用 <title> 标签重命名 HTML 文件
【发布时间】:2013-12-21 18:45:26
【问题描述】:

我对编程比较陌生。我有一个带有子文件夹的文件夹,其中包含数千个通常命名的 html 文件,即 1006.htm、1007.htm,我想使用文件中的标签重命名它们。

例如,如果文件 1006.htm 包含 Page Title ,我想将其重命名为 Page Title.htm。理想情况下,空格替换为破折号。

我一直在使用 bash 脚本在 shell 中工作,但没有运气。如何使用 bash 或 python 做到这一点?

这是我目前所拥有的......

#!/usr/bin/env bashFILES=/Users/Ben/unzipped/*
for f in $FILES
do
   if [ ${FILES: -4} == ".htm" ]
      then
    awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' $FILES
   fi
done

我也试过

#!/usr/bin/env bash
for f in *.html;
   do
   title=$( grep -oP '(?<=<title>).*(?=<\/title>)' "$f" )
   mv -i "$f" "${title//[^a-zA-Z0-9\._\- ]}".html   
done

但是我从终端收到一个错误,说明如何使用 grep...

【问题讨论】:

  • 所以我们到目前为止你做了什么。我认为 grep 类型的解决方案可能有效

标签: python html bash scrape renaming


【解决方案1】:

在你的 bash 脚本中使用 awk 而不是 grep,它应该可以工作:

#!/bin/bash   
for f in *.html;
   do
   title=$( awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' "$f" )
   mv -i "$f" "${title//[^a-zA-Z0-9\._\- ]}".html   
done

不要忘记在第一行更改您的 bash 环境;)

编辑所有修改的完整答案

#!/bin/bash
for f in `find . -type f | grep \.html`
   do
   title=$( awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' "$f" )
   mv -i "$f" "${title//[ ]/-}".html
done

【讨论】:

  • 这很好用——知道如何将它应用于子目录中的所有文件吗?并在 标签中有空格的地方添加一个“-”?
  • 找到 . -类型 f | grep \.html 获取所有 html 文件
  • 对于 '-' 替换,我建议你这样做: mv -i "$f" "${title//[ ]/-}".html
  • find . -type f | grep \.htm 打印出终端目录中的所有文件——但我如何将其传递给脚本的其余部分?
  • 我已经用完整的脚本编辑了我的答案!下次请在开头提供所有详细信息。谢谢!
【解决方案2】:

您想使用 HTML 解析器(如lxml.html)来解析您的 HTML 文件。一旦你得到它,检索标题标签是一行(可能是page.get_element_by_id("title").text_content())。

将其转换为文件名并重命名文档应该很简单。

【讨论】:

    【解决方案3】:

    这是我刚刚写的一个python脚本:

    import os
    import re
    
    from lxml import etree
    
    
    class MyClass(object):
        def __init__(self, dirname=''):
            self.dirname   = dirname
            self.exp_title = "<title>(.*)</title>"
            self.re_title  = re.compile(self.exp_title)
    
        def rename(self):
            for afile in os.listdir(self.dirname):
                if os.path.isfile(afile):
                    originfile = os.path.join(self.dirname, afile)
                    with open(originfile, 'rb') as fp:
                        contents = fp.read()
                    try:
                        html  = etree.HTML(contents)
                        title = html.xpath("//title")[0].text
                    except Exception as e:
                        try:
                            title = self.re_title.findall(contents)[0]
                        except Exception:
                            title = ''
    
                    if title:
                        newfile = os.path.join(self.dirname, title)
                        os.rename(originfile, newfile)
    
    
    >>> test = MyClass('/path/to/your/dir')
    >>> test.rename()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 2020-12-29
      • 2014-01-31
      • 2020-02-13
      相关资源
      最近更新 更多