sed命令从HTML中提取文本 [英] sed command to extract text from HTML

查看:257
本文介绍了sed命令从HTML中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取使用curl的网页的来源,并且想要从特定标记中提取文字。



文字位于唯一标记之间:

  href =http://www.website.com/some/unique/page.php?q=xyz> TEXT< ; / a> 

我做的是:

  curl -shttp://www.website.com| sed's | PATTERN | \1 |'

其中PATTERN是许多正则表达式模式之一已尝试过,其中之一:

  href =http://www.website.com/some/unique/page。 php?q = xyz> \(。* \)< / a> 

但是由于某些原因,我无法使用它。我可以得到完整的页面源或没有什么(根据我使用的模式)。



感谢任何帮助。

TEXT ,这将使用你输入的输入:

  sed's /^.*> \([^<] * \)<。* $ / \1 / '

如果只需要输出 TEXT ,并且您只希望从其路径中包含单词 unique 的URL输出,然后改用:

  sed -n'/http:.*\/unique\//s/^.*>\([^<] * \)<。* $ / \1 / p'


I am grabbing the source of a page useing curl, and want to extract a text from a specific tag.

the text is between the unique tag:

href="http://www.website.com/some/unique/page.php?q=xyz">TEXT</a>

What I did was:

curl -s "http://www.website.com" | sed 's|PATTERN|\1|'

Where PATTERN is one of the many regex patterns I have tried, one of which:

href="http://www.website.com/some/unique/page.php?q=xyz">\(.*\)</a>

But for some reasons, I couldn't get it to work. I either get the full page source or nothing at all (depending on the pattern I used).

Would appreciate any help.

解决方案

Assuming your desired output is just TEXT, this will work with the input you gave:

sed 's/^.*>\([^<]*\)<.*$/\1/'

If the only output you want is TEXT and you only want that to be output from a URL containing the word unique in it's path then use this instead:

sed -n '/http:.*\/unique\//s/^.*>\([^<]*\)<.*$/\1/p'

这篇关于sed命令从HTML中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆