1

A friend and I are trying to learn jQuery, and we've come across a problem we just can't figure out. We're trying to use WhateverOrigin to scrape some data from a forum (we have permission to do so from the owner, he set up a test post for us to practice scraping on). The HTML we're working on is this:

<div>
  <span id="msg_68" class="subject_title">
    <a href="[insert link here]">TEST: SCRAPE THE URL</a>
  </span>
</div>

Using WhateverOrigin, we can successfully pull the complete HTML of the site using

$.getJSON('http://www.whateverorigin.org/get?url=' + encodeURIComponent('[INSERT URL HERE]') + '&callback=?', function(data){
    alert(data.contents);
});

However, when we tried to pull that specific element's HTML or text (to check we were pulling the correct data) we either got "undefined" with html or "" with text.

The code we were using to pull it was this:

$(document).ready(function(){
  $.getJSON('http://www.whateverorigin.org/get?url=' + encodeURIComponent('[INSERT URL HERE]') + '&callback=?', function(data){
      alert($("#msg_68 a").text());
      alert($("#msg_68 a").html());
  });
});

any ideas?

Thanks

2 Answers 2

1

When you write $("#msg_68 a"), you are trying to access this element from the DOM in your current page and not from the loaded data.

You need to select #msg_68 a elements, within the data.contents, you can either :

Parse this data into a DOM element then fetch it to get the required element:

$.getJSON('http://www.whateverorigin.org/get?url=' + encodeURIComponent('[INSERT URL HERE]') + '&callback=?', function(data){
    var div = $("<div><div/>");
    div.html(data.contents);
    alert($("#msg_68 a", div).text());
});

Or just refer to it directly with data.contents as the content is a valid HTML:

alert($("#msg_68 a", data.contents).text());
Sign up to request clarification or add additional context in comments.

Comments

0

By using $("#msg_68 a") you are selecting an element from the DOM. That element is not in the DOM yet, it is stored in a variable at that moment after you get the data from ajax. To select it like that, you'd have to first insert data from ajax to your DOM. Your alternative is the "filter" function or searching within the 'data' variable context (depending on your jquery version)

Option 1:

$(data.contents).filter('#msg_68 a');

Option 2:

$("#msg_68 a", data.contents);

4 Comments

I still get back an empty string and undefined, even after putting that in. Any other ideas?
@Josh, what exactly do you get in data output?
I get an empty alert for the .text() call and "undefined" from the .html() call
Sorry, my bad. Change the data to data.contents and it should work ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.