【问题标题】:Convert an HTML expandable list into tabular or csv format将 HTML 可扩展列表转换为表格或 csv 格式
【发布时间】:2014-05-03 08:10:32
【问题描述】:

我知道这个话题已经讨论过几次,但我找不到适用于我的案例。我不是一个有经验的计算机用户,请记住这一点,虽然我可以玩 bash、R 并且可能也运行 perl 脚本。仅供参考 - 我在我的机器上运行 Ubuntu。

我想做的是将以下网页http://www.genome.jp/kegg-bin/get_htext?br08902.keg的可展开列表(请使用“一键模式”完全展开)转换为表格或csv格式,其中每个级别的缩进到一个单独的列。

对于分组在其下方的所有元素重复父类别也不会那么糟糕。类似于下面我为页面的前几行手动制作的选项卡。

Pathways and Ontologies Pathways    br08901  KEGG pathway maps
Pathways and Ontologies Functional hierarchies  br08902  BRITE functional hierarchies
Genes and Proteins  Orthologs and modules   ko00001  KEGG Orthology (KO)
Genes and Proteins  Orthologs and modules   ko00002  KEGG pathway modules
Genes and Proteins  Orthologs and modules   ko00003  KEGG modules and reaction modules
Genes and Proteins  Protein families: metabolism    ko01000  Enzymes
Genes and Proteins  Protein families: metabolism    ko01001  Protein kinases
Genes and Proteins  Protein families: metabolism    ko01009  Protein phosphatases and associated proteins
Genes and Proteins  Protein families: metabolism    ko01002  Peptidases
Genes and Proteins  Protein families: metabolism    ko01003  Glycosyltransferases
Genes and Proteins  Protein families: metabolism    ko01005  Lipopolysaccharide biosynthesis proteins
Genes and Proteins  Protein families: metabolism    ko01004  Lipid biosynthesis proteins

提前致谢!

【问题讨论】:

  • ...到目前为止,您尝试解析什么?哪里不行?

标签: html bash parsing csv


【解决方案1】:

此任务需要一些分开的步骤。细分:

获取页面内容。您可以使用例如curlwgetfetch 或类似程序。例如

curl http://...

将下载页面内容。

在您的页面中,存在一个链接“下载 htext”。当您检查它指向的位置时,您会发现从链接中下载的不是您所需要的

http://www.kegg.jp/kegg-bin/download_htext?htext=br08902.keg&format=htext&filedir=
                                                 ^^^^^^^^^^^ name of your needed keg

所以之后

curl "http://www.kegg.jp/kegg-bin/download_htext?htext=br08902.keg&format=htext&filedir=" > mykeg.txt

会得到一个看起来像下一个的文件:(缩短)

+C      Br number
#<h2><a href="/kegg/kegg2.html"><img src="/Fig/bget/kegg3.gif" align="middle" border=0></a>&nbsp; BRITE Functional Hierarchies</h2>
#<!---
#ENTRY       br08902
#NAME        Brite
#DEFINITION  BRITE functional hierarchies
#--->
!
A<b>Pathways and Ontologies</b>
B  Pathways
C    br08901  KEGG pathway maps
B  Functional hierarchies
C    br08902  BRITE functional hierarchies
#
A<b>Genes and Proteins</b>
B  Orthologs and modules
C    ko00001  KEGG Orthology (KO)
C    ko00002  KEGG pathway modules

这是一个不错的文本文件,大部分没有 HTML 标记。可使用常用 bash 工具轻松解析。

首先进行一些清理:

使用sed 命令删除所有不需要的行

sed '/^[#!+]/d'

删除不需要的 html 标记(使用正则表达式通常是不可能的,但在这种情况下可能)

sed 's/<[^>]*>//g'

为前导字符添加分隔符

sed 's/^./& /'

在上面之后,你会得到一个类似下一个的文本

A Pathways and Ontologies
B   Pathways
C     br08901  KEGG pathway maps
B   Functional hierarchies
C     br08902  BRITE functional hierarchies
A Genes and Proteins
B   Orthologs and modules
C     ko00001  KEGG Orthology (KO)
C     ko00002  KEGG pathway modules
C     ko00003  KEGG modules and reaction modules

什么是 bash 有一个很好的、可解析的结构

while read -r prefix content
do
     echo "do something with a line >>$content<< with a prefix >>$prefix<<"
done

您可以使用case 命令测试prefix,例如:

case "$prefix" in
    A) a="$content" ;;
    B) b="$content" ;;
    C) c="$content" ;;
esac

使用associative arrays 有一个更好的替代方案,但上面的方法很简单且有效...

您现在拥有制作有效解决方案所需的所有信息(8 行)。

下一个由你决定... ;)

编辑

通常我不会做全部工作,因为 stackoverflow 不是免费的编程服务,但可以 - 这是脚本:

kegfile="KEG"
while read -r prefix content
do
    case "$prefix" in
        A) col1="$content" ;;
        B) col2="$content" ;;
        C) echo -e "$col1\t$col2\t$content";;
    esac
done < <(sed '/^[#!+]/d;s/<[^>]*>//g;s/^./& /' < "$kegfile")

【讨论】:

  • @jm666 - 感谢您非常详细的回复。我正在尝试按照您的指示进行操作,但是当我尝试使用 curl 下载时,它会创建一个空的 .txt 文件。我已经运行了以下curl "http://www.kegg.jp/kegg-bin/download_htext?htext=q00001.keg&amp;format=htext&amp;filedir=" &gt; KEGG.txt。我也尝试过 wget ,它是一样的。知道那会是什么吗?包含我的结果的主页是以下genome.jp/kegg-bin/get_htext?q00001.keg+-p+/tools/kaas/files/…,如果您想自己尝试,可能实际页面有问题??
  • 当您在浏览器中单击“下载 htext”时,什么也没有发生(既没有下载也没有在浏览器中)。所以,这可能是实际页面的问题。
  • 确实,我注意到我发送给您的另一个链接 htext 当您单击它时实际上会下载一个文件。我会先尝试解决这个问题,但我相信你的解决方案会奏效!再次感谢。
  • 我没有得到任何文件,也许服务器有一些超时或限制等。在最坏的情况下,您可以使用浏览器下载“htext”并将脚本仅用于处理......
  • @ jm666 - 我成功了。不过我有几个问题。当我使用 sed 进行清理时,应仅输入文件名,例如sed '/[^#!+]/d' mykeg.txt 还是我应该把它发送到&gt; newfile.txt?第二个选项似乎没有产生任何重大影响,而第一个选项似乎在 bash 中运行,但我不确定它是否也修改了主文件(对我来说似乎相同)。抱歉,我以前从未使用过此功能,无法从帮助中绕过它。我也不确定解析的第二部分,我应该按原样输入还是根据我的需要进行修改?我不太确定。 :)
猜你喜欢
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 2010-11-27
  • 1970-01-01
  • 2021-03-22
相关资源
最近更新 更多