-1

I'm scraping the source code of a website.

My first print prints out the complete source code.

Then the second print prints an actual DOM to the console, but for some reason the contents of the document change just slightly.

A thing that bugs me is that the <body> tag goes missing and I have no idea why.

I just realized the <head> tag goes missing as well. So there might be a good reason for it.

TO CLARIFY: The content of both the <head> and <body> tags remain together in the container. Just the tags themselves disappear, not their content.

I want the whole source code to be parsed into an accessible DOM.

This is the code:

$.ajax({url: url, dataType: "text", success: function(data) { console.log("data:", data); var htmlDocument = $("<html>").html(data)[0]; console.log("htmlDocument:", htmlDocument); }}); 

I am new to JavaScript, thank you for any help. I am eager to understand the issue but for now I really just want it to work.

13
  • 1
    What exactly do you want to do with it? Please elaborate on use case a bit more. Note that jQuery html() removes <body> and <head> Commented Aug 5, 2018 at 16:54
  • i want to access the body tag. search its content. i just realized the <head> tag is missing as well. why is that? Commented Aug 5, 2018 at 16:55
  • If all you want is to search through it do var $content = $('<div>').html(data); then can use find() on $content ... console.log($content.find('div').length) for example will find count of div from other page Commented Aug 5, 2018 at 16:57
  • whats the difference betweens $('<div>').html(data);and $('<html>').html(data);? i would prefer to have an exact copy of the source code accessible as a DOM. Commented Aug 5, 2018 at 17:01
  • No real need to use <html> since all you said you want to do is look in content Commented Aug 5, 2018 at 17:02

1 Answer 1

1

As Charlietfl said

Note that jQuery .html() removes body and head

Try

 $('html')[0].outerHTML 

or

document.documentElement.outerHTML 

See more here: How do I get the entire page's HTML with jQuery?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.