【发布时间】:2012-03-20 01:05:48
【问题描述】:
我正在为自己的谷歌搜索制作一个 CLI,并且在开发聊天机器人时使用 nodejs 已经有一段时间了,所以我想让它与 nodejs 一起使用。我可以很好地提取数据,并最终得到一个包含页面中所有 html 数据的字符串。甚至可以很容易地在 html 中整理出我想要的结果是什么:
<div class="jd"><a class="p" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://leagueoflegends.com/&ved=0CBAQFjAA&usg=AFQjCNEEnWGHwxNnuwKenqm4ajKfTM6Xxw" ><b>League of Legends</b> - Free Online Game | <b>LoL</b> - <b>League of Legends</b></a> </div> <div class="kd">3 days ago … Official website for <b>League of Legends</b>. Join millions of players in an award winning Multiplayer Online Battle Arena. </div> <div class="qdlmxn"><a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://leagueoflegends.com/board&ved=0CBEQ0gIoADAA&usg=AFQjCNHpmmAdFFbTgm8C_gJvsjVhMzVKUQ" >Community</a> - <a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://signup.leagueoflegends.com/en/signup/redownload&ved=0CBIQ0gIoATAA&usg=AFQjCNFHGUtn4ItgQIzODgIZRv_237Mq0A" >PVP.NET</a> - <a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://na.leagueoflegends.com/board/forumdisplay.php?f%3D2&ved=0CBMQ0gIoAjAA&usg=AFQjCNHpycJ8WGh7xvWw1qNu8NjjU1EA0Q" >General Discussion</a> - <a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://na.leagueoflegends.com/champions&ved=0CBQQ0gIoAzAA&usg=AFQjCNEpeBzNefwag5xmkFcFhCW27FoAew" >Champions</a> </div><span class="c">leagueoflegends.com/</span> - <div class="txnles" onclick="_popup('web_result_popup_10836585','inline');"> <div class="wx4xyp" id="web_result_popup_10836585"> <div class="vfc7iu"><a class="s" href="/search?q=cache:GCRD1wy5e3QJ:leagueoflegends.com/" >Cached</a> <br/><a class="s" href="/m/?q=related:leagueoflegends.com/&ei=ZtZnT8CTOMy48AbDzgE&ved=0CBYQHzAA" >Similar</a> <br/><a class="s" href="/gwt/x?q=lol&ei=ZtZnT8CTOMy48AbDzgE&hl=en&source=m&u=http://leagueoflegends.com/" >Mobile formatted</a> </div> </div><a class="s" href="javascript:void(0)" >Options</a> <div class="m6u8fq"> </div> </div> </div> </div> <div> <div class="r ld"> <div class="jd"><a class="p" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://en.wikipedia.org/wiki/LOL&ved=0CBgQFjAB&usg=AFQjCNFOhgg5Y2E5SFuS5I-8830OJ9VR9Q" ><b>LOL</b> - Wikipedia, the free encyclopedia</a> </div> <div class="kd"><b>LOL</b>, an abbreviation for <b>laughing out loud</b>, or <b>laugh out loud</b>, is a common element of Internet slang. It was used historically on Usenet … </div><span class="c">en.wikipedia.org/wiki/LOL</span> - <div class="txnles" onclick="_popup('web_result_popup_30597472','inline');"> <div class="wx4xyp" id="web_result_popup_30597472"> <div class="vfc7iu"><a class="s" href="/search?q=cache:mhIpOeXQp38J:en.wikipedia.org/wiki/LOL" >Cached</a> <br/><a class="s" href="/m/?q=related:en.wikipedia.org/wiki/LOL&ei=ZtZnT8CTOMy48AbDzgE&ved=0CBkQHzAB" >Similar</a> <br/><a class="s" href="/gwt/x?q=lol&ei=ZtZnT8CTOMy48AbDzgE&hl=en&source=m&u=http://en.wikipedia.org/wiki/LOL" >Mobile formatted</a> </div> </div><a class="s" href="javascript:void(0)" >Options</a> <div class="m6u8fq"> </div> </div> </div> </div> <div> <div class="r ld">
任何 .jd 都是结果,所以我首先需要将它们分开,然后将 URL 和描述也分开。我从来没有做过这么极端的字符串操作,所以我不知道从哪里开始。
这是更易读格式的 html,尽管我处理的是一个长字符串。
<div>
<div class="r ld">
<div class="jd">
<a class="p" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://leagueoflegends.com/&ved=0CBAQFjAA&usg=AFQjCNEEnWGHwxNnuwKenqm4ajKfTM6Xxw" >
<b>League of Legends</b> - Free Online Game | <b>LoL</b> - <b>League of Legends</b>
</a>
</div>
<div class="kd">
3 days ago … Official website for <b>League of Legends</b>. Join millions of players in an award winning Multiplayer Online Battle Arena.
</div>
<div class="qdlmxn">
<a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://leagueoflegends.com/board&ved=0CBEQ0gIoADAA&usg=AFQjCNHpmmAdFFbTgm8C_gJvsjVhMzVKUQ" >Community</a> -
<a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://signup.leagueoflegends.com/en/signup/redownload&ved=0CBIQ0gIoATAA&usg=AFQjCNFHGUtn4ItgQIzODgIZRv_237Mq0A" >PVP.NET</a> -
<a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://na.leagueoflegends.com/board/forumdisplay.php?f%3D2&ved=0CBMQ0gIoAjAA&usg=AFQjCNHpycJ8WGh7xvWw1qNu8NjjU1EA0Q" >General Discussion</a> -
<a class="gg" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://na.leagueoflegends.com/champions&ved=0CBQQ0gIoAzAA&usg=AFQjCNEpeBzNefwag5xmkFcFhCW27FoAew" >Champions</a>
</div>
<span class="c">leagueoflegends.com/</span> -
<div class="txnles" onclick="_popup('web_result_popup_10836585','inline');">
<div class="wx4xyp" id="web_result_popup_10836585"> <div class="vfc7iu">
<a class="s" href="/search?q=cache:GCRD1wy5e3QJ:leagueoflegends.com/" >Cached</a>
<br/>
<a class="s" href="/m/?q=related:leagueoflegends.com/&ei=ZtZnT8CTOMy48AbDzgE&ved=0CBYQHzAA" >Similar</a>
<br/>
<a class="s" href="/gwt/x?q=lol&ei=ZtZnT8CTOMy48AbDzgE&hl=en&source=m&u=http://leagueoflegends.com/" >Mobile formatted</a>
</div>
</div>
<a class="s" href="javascript:void(0)" >Options</a>
<div class="m6u8fq"> </div>
</div>
</div>
</div>
<div>
<div class="r ld">
<div class="jd">
<a class="p" href="/m/url?ei=ZtZnT8CTOMy48AbDzgE&q=http://en.wikipedia.org/wiki/LOL&ved=0CBgQFjAB&usg=AFQjCNFOhgg5Y2E5SFuS5I-8830OJ9VR9Q" >
<b>LOL</b> - Wikipedia, the free encyclopedia
</a>
</div>
<div class="kd">
<b>LOL</b>, an abbreviation for <b>laughing out loud</b>, or <b>laugh out loud</b>, is a common element of Internet slang. It was used historically on Usenet …
</div>
<span class="c">en.wikipedia.org/wiki/LOL</span> -
<div class="txnles" onclick="_popup('web_result_popup_30597472','inline');">
<div class="wx4xyp" id="web_result_popup_30597472">
<div class="vfc7iu">
<a class="s" href="/search?q=cache:mhIpOeXQp38J:en.wikipedia.org/wiki/LOL" >Cached</a>
<br/>
<a class="s" href="/m/?q=related:en.wikipedia.org/wiki/LOL&ei=ZtZnT8CTOMy48AbDzgE&ved=0CBkQHzAB" >Similar</a>
<br/>
<a class="s" href="/gwt/x?q=lol&ei=ZtZnT8CTOMy48AbDzgE&hl=en&source=m&u=http://en.wikipedia.org/wiki/LOL" >Mobile formatted</a>
</div>
</div>
<a class="s" href="javascript:void(0)" >Options</a>
<div class="m6u8fq"> </div>
</div>
</div>
</div>
【问题讨论】:
-
你为什么要粘贴一个可怕的 HTML 行?您认为我们可以轻松阅读吗?请将其格式化为可用状态。
-
这正是我得到的,这就是我正在使用的。为什么要发布与问题无关的内容?
-
为什么不用jsDom来操作DOM节点呢?
-
我打算把它作为一个命令行工具,所以我可以直接跳到我的终端并说“google 'terms'”并让它返回结果,并最终将它移动到聊天机器人。
-
@JamWaffles 我看不出能够更轻松地阅读它有什么帮助,但它确实存在。
标签: javascript html parsing node.js