write to json file

This commit is contained in:
2021-09-28 18:44:04 +08:00
parent 4339a99a65
commit 3d350b6090
5 changed files with 28 additions and 5 deletions
+1
View File
@@ -14,6 +14,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation group: 'org.jsoup', name: 'jsoup', version: '1.14.2'
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.5'
}
test {
+10 -4
View File
@@ -2,13 +2,19 @@ package cmy.newsspider;
import cmy.newsspider.searchpageextractor.SearchPageExtractor;
import cmy.newsspider.searchpageextractor.SogouSearchPageExtractor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
SearchPageExtractor spe = new SogouSearchPageExtractor();
var result = spe.searchByKeyword("限电", 20);
System.out.println(result);
var keyword = "限电";
var result = spe.searchByKeyword(keyword, 20);
var mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(Paths.get(String.format("%s-result.json", keyword)).toFile(), result);
}
}
@@ -39,6 +39,9 @@ public class IFengExtractor extends PageExtractor {
var dateElement = pageDoc.select("div[class^=artical] div[class^=info-] p[class^=time]>span");
if (dateElement.isEmpty()) {
dateElement = pageDoc.select("span[itemprop=datePublished]");
if (dateElement.isEmpty()) {
return "";
}
}
return dateElement.get(0).text().strip();
}
@@ -1,4 +1,14 @@
package cmy.newsspider.record;
public record NewsRecord(String title, String content, String source, String date) {
import com.fasterxml.jackson.annotation.JsonProperty;
public record NewsRecord(
@JsonProperty("title")
String title,
@JsonProperty("content")
String content,
@JsonProperty("source")
String source,
@JsonProperty("time")
String time) {
}
+3
View File
@@ -1,4 +1,7 @@
module cmy.newsspider {
exports cmy.newsspider.record to com.fasterxml.jackson.databind;
requires org.jsoup;
requires jdk.crypto.ec;
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
}