James W James W - 1 year ago 36
Javascript Question

How can I extract data from a webpage when the data has no selectors?

I am writing some JavaScript to extract data from a third-party website. The data that I need to extract is formatted as such:

<!-- a bunch of extra stuff before this -->
<td>Keyword: </td>
Other stuff I don't care about
<!-- a bunch of extra stuff after this -->

What I'd like to do is search the DOM for "Keyword:" and then grab "Value" from the next TD.

Answer Source

jQuery's :contains() selector would do this.

For your specific example:

$( "td:contains('Keyword: ')" ).next().contents()[0].val();

First it finds that contains "Keyword: ". Then it finds the next element, then it splits that into an array of parts, then it selects the first element in that array, then gets the value of that element.