Python 快速解析站点地图
我有30个站点地图文件,如下所示:Python 快速解析站点地图,python,sed,awk,sitemap,Python,Sed,Awk,Sitemap,我有30个站点地图文件,如下所示: <?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.A.com/a</loc> <lastmod>2013-08-01</lastmod> <changefre
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.A.com/a</loc>
<lastmod>2013-08-01</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
<url>
<loc>http://www.A.com/b</loc>
<lastmod>2013-08-01</lastmod>
<changefreq>weekly</changefreq>
<priority>0.6</priority>
</url>
...
</urlset>
我使用的方法是Python BeautifulSoup来解析标记,但是,性能非常慢,因为那里有30多个文件,每个文件有300000行。我想知道是否可以使用一些shell AWK或SED来实现这一点,或者。。我只是用错了工具
由于站点地图的格式非常好,因此可能有一些正则表达式技巧可以绕过它
有没有人有过将AWK或SED中的记录/行划分为多行而不是新行字符的经验
非常感谢 我绝对不建议将正则表达式作为解析任意XML或HTML的常规方法,但既然您说这是一种格式非常好的方法,那么在这种情况下,可能可以忽略以下内容:
sed -n '/^<url>$/{n;N;N;N;s/\n/ /g;s/ *<[a-z]*>//g;s/<\/[a-z]*>/ /g;p}'
sed-n'/^$/{n;n;n;s/\n//g;s/////g;s///g;p}'
下面是一个注释版本,解释了发生的情况:
sed -n '/^<url>$/ { # if this line contains only <url>
n;N;N;N # read the next 4 lines into the pattern space
s/\n//g # remove newlines
s/ *<[a-z]*>//g # remove opening tags and the spaces before them
s/<\/[a-z]*>/ /g # replace closing tags with a space
p # print the pattern space
}' test.txt
sed-n'/^$/{#如果此行仅包含
n、 n;n;n#将接下来的4行读入模式空间
s/\n//g#删除换行符
s/*//g#删除开头标记及其前面的空格
s///g#用空格替换结束标记
打印图案空间
}'test.txt
-n
选项禁止模式空间的自动打印。我绝对不建议将正则表达式作为解析任意XML或HTML的常规方法,但由于您说过这是一种格式非常好的方法,因此在这种情况下可能会忽略:
sed -n '/^<url>$/{n;N;N;N;s/\n/ /g;s/ *<[a-z]*>//g;s/<\/[a-z]*>/ /g;p}'
sed-n'/^$/{n;n;n;s/\n//g;s/////g;s///g;p}'
下面是一个注释版本,解释了发生的情况:
sed -n '/^<url>$/ { # if this line contains only <url>
n;N;N;N # read the next 4 lines into the pattern space
s/\n//g # remove newlines
s/ *<[a-z]*>//g # remove opening tags and the spaces before them
s/<\/[a-z]*>/ /g # replace closing tags with a space
p # print the pattern space
}' test.txt
sed-n'/^$/{#如果此行仅包含
n、 n;n;n#将接下来的4行读入模式空间
s/\n//g#删除换行符
s/*//g#删除开头标记及其前面的空格
s///g#用空格替换结束标记
打印图案空间
}'test.txt
-n
选项禁止自动打印图案空间。sed是一个非常好的工具,用于在单行上进行简单替换,对于任何其他内容,只需使用awk:
$ awk -F'[<>]' '
/^<\/url>/ { inUrl=0; print line }
inUrl { line = line (line?" ":"") $3 }
/^<url>/ { inUrl=1; line="" }
' file
http://www.A.com/a 2013-08-01 weekly 0.6
http://www.A.com/b 2013-08-01 weekly 0.6
$awk-F'[]
/^/{inUrl=0;打印行}
inUrl{line=line(line?“:”)$3}
/^/{inUrl=1;line=”“}
"档案"
http://www.A.com/a 2013-08-01每周0.6
http://www.A.com/b 2013-08-01每周0.6
sed是一个非常好的工具,可以在一行上进行简单的替换,对于其他任何内容,只需使用awk:
$ awk -F'[<>]' '
/^<\/url>/ { inUrl=0; print line }
inUrl { line = line (line?" ":"") $3 }
/^<url>/ { inUrl=1; line="" }
' file
http://www.A.com/a 2013-08-01 weekly 0.6
http://www.A.com/b 2013-08-01 weekly 0.6
$awk-F'[]
/^/{inUrl=0;打印行}
inUrl{line=line(line?“:”)$3}
/^/{inUrl=1;line=”“}
"档案"
http://www.A.com/a 2013-08-01每周0.6
http://www.A.com/b 2013-08-01每周0.6
这可能适合您(GNU-sed):
这可能适用于您(GNU-sed):
老兄,你摇滚,你能解释一点你疯狂的正则表达式吗?刚刚添加了一个注释版本。你摇滚,你能解释一点你疯狂的正则表达式吗?刚刚添加了一个注释版本