erstellen Ich habe das folgende HTML-Snippet. Ich möchte die Seite scrapen, um die Themen und Unterthemen zu erhalten und sie in Objekten zu speichern.Wie hierarchische Objekte mit Geschwister-Tags mit jquery Selektoren
das gewünschte Ergebnis ist etwas:
{
'topic': 'Java Basics',
'subtopics':['Define the scope of variables', 'Define the structure of a Java class', ...]
}
ich versucht, es mit Jsdom für Node.js und JQuery funktioniert:
var jsdom = require('jsdom');
var fs = require("fs");
var topicos = fs.readFileSync("topic.html", "utf-8");
jsdom.env(topicos, ["http://code.jquery.com/jquery.js"], function (error, window) {
var $ = window.$;
var length = $('div ~ ').each(function() {
//???
var topic = $(this);
var text = topic.text();
console.log(text.trim())
});
})
aber aufgrund meiner mangelnden Erfahrung in jQuery, ich Ich bin nicht in der Lage, die Hierarchie richtig zu organisieren.
HTML-Schnipsel:
<div>
<strong>Java Basics </strong></div>
<ul>
<li>
Define the scope of variables </li>
<li>
Define the structure of a Java class
</li>
<li>
Create executable Java applications with a main method; run a Java program from the command line; including
console output.
</li>
<li>
Import other Java packages to make them accessible in your code
</li>
<li>
Compare and contrast the features and components of Java such as:
platform independence, object orientation, encapsulation, etc.
</li>
</ul>
<div>
<strong>Working With Java Data Types </strong></div>
<ul>
<li>
Declare and initialize variables (including casting of primitive data types)
</li>
<li>
Differentiate between object reference variables and primitive variables
</li>
<li>
Know how to read or write to object fields
</li>
<li>
Explain an Object's Lifecycle (creation, "dereference by reassignment" and garbage collection)
</li>
<li>
Develop code that uses wrapper classes such as Boolean, Double, and Integer. </li>
</ul>
...