简体   繁体   English

jQuery/NodeJS:获取没有唯一 class 名称的按钮的 URL

[英]jQuery/NodeJS: Getting URL of a button without unique class name

So I'm trying to get a certain link of a button on a website I parse via NodeJS:所以我试图在我通过NodeJS解析的网站上获取某个按钮的链接:

Usually, it would be fairly easy when using cheerio:通常,使用cheerio 会相当容易:

$('.panel-default').find('.btn-default').attr('href');

The problem is: There are 3 other buttons with the same classname, on overall 60 items:问题是:有 3 个其他按钮具有相同的类名,总共 60 个项目:

<a href="https://someurl.com" class="btn btn-xs btn-default" target="_blank"><span class="fa fa-fw fa-steam-square poptip" data-toggle="tooltip" title="" data-original-title="View Details"></span></a>

<a href="/somedir" class="btn btn-xs btn-default"><span class="fa fa-road fa-fw poptip" data-toggle="tooltip" title="" data-original-title="See more like this"></span></a>

<a href="/otherdir" class="btn btn-xs btn-default" target="_blank"><span class="fa fa-link fa-fw poptip" data-toggle="tooltip" title="" data-original-title="View shareable link"></span></a>

So looping through the 4 results with.each, picking the right one, and repeat that on all 60 other items don't seem like an elegant choice for me.因此,使用.each 循环遍历 4 个结果,选择正确的一个,然后在所有 60 个其他项目上重复这一点,这对我来说似乎不是一个优雅的选择。

Is there any other way to grab the wanted info, for example by calling the unique data-original-title "Inspect in game" via cheerio (is that even possible?) for example?有没有其他方法可以获取想要的信息,例如通过cheerio 调用独特的数据原始标题“Inspect in game”(这甚至可能吗?)? Or how else could this be solved in an elegant and smooth way?或者如何以优雅而流畅的方式解决这个问题?

If $('.panel-default').find('.btn-default') return more than one value, you can pass index that you want to get just her href attribute.如果$('.panel-default').find('.btn-default')返回一个以上的值,你可以通过 index 来获取她的 href 属性。

If you want the second element href by example:如果你想要第二个元素 href 的例子:

$('.panel-default').find('.btn-default')[1].attr('href');

Use the data selector.使用data选择器。

$('.panel-default').find(".btn-default[data-original-title='Microsoft']").attr('href');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM