File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import (
1010
1111func TestSetAndSavePost (t * testing.T ) {
1212 // p := PostFactory("https://www.dwnews.com/经济/60203253")
13- p := PostFactory ("https://www.dwnews.com/经济/60203034 " ) // The wrong one
13+ p := PostFactory ("https://www.rfa.org/mandarin/Xinwen/6-07082020110802.html " ) // The wrong one
1414 raw , doc , err := htmldoc .GetRawAndDoc (p .URL , 1 * time .Minute )
1515 if err != nil {
1616 t .Errorf ("GetRawAndDoC error: %v" , err )
@@ -30,6 +30,7 @@ func TestTreatPost(t *testing.T) {
3030 "https://www.dwnews.com/全球/60203234" ,
3131 "https://www.voachinese.com/a/S-Korea-Says-US-Sees-Importance-Of-N-Korea-Talks-Despite-Tension-20200709/5496028.html" ,
3232 "https://www.rfa.org/mandarin/yataibaodao/shaoshuminzu/gf1-07092020074142.html" ,
33+ "https://www.rfa.org/mandarin/Xinwen/6-07082020110802.html" ,
3334 }
3435 for _ , tc := range tcs {
3536 p := PostFactory (tc )
Original file line number Diff line number Diff line change @@ -91,7 +91,8 @@ func Rfa(p *Post) (string, error) {
9191 for _ , v := range plist {
9292 if v .FirstChild == nil {
9393 continue
94- } else if v .FirstChild .Data == "b" {
94+ }
95+ if v .FirstChild .Data == "b" {
9596 body += "** "
9697 blist := htmldoc .ElementsByTagName (v , "b" )
9798 for _ , b := range blist {
@@ -102,7 +103,13 @@ func Rfa(p *Post) (string, error) {
102103 }
103104 body += " ** \n "
104105 } else {
105- body += v .FirstChild .Data + " \n "
106+ // body += v.FirstChild.Data + " \n"
107+ innerNodes := htmldoc .ElementsNext (v )
108+ for _ , in := range innerNodes {
109+ if in .Type == html .TextNode {
110+ body += in .Data + " \n "
111+ }
112+ }
106113 }
107114 }
108115 body = strings .ReplaceAll (body , "** ** \n " , "" )
Original file line number Diff line number Diff line change 1+ package rfa
2+
3+ import (
4+ "fmt"
5+ "log"
6+ "net/url"
7+ "testing"
8+ "time"
9+
10+ "github.com/wedojava/fetcher/internal/htmldoc"
11+ )
12+
13+ func PostFactory (rawurl string ) * Post {
14+ url , err := url .Parse (rawurl )
15+ if err != nil {
16+ log .Printf ("url parse err: %s" , err )
17+ }
18+ return & Post {
19+ Domain : url .Hostname (),
20+ URL : url ,
21+ }
22+ }
23+
24+ func TestSetPost (t * testing.T ) {
25+ p := PostFactory ("https://www.rfa.org/mandarin/Xinwen/6-07082020110802.html" )
26+ raw , doc , err := htmldoc .GetRawAndDoc (p .URL , 1 * time .Minute )
27+ if err != nil {
28+ t .Errorf ("GetRawAndDoc err: %v" , err )
29+ }
30+ p .Raw , p .DOC = raw , doc
31+ if err := SetPost (p ); err != nil {
32+ t .Errorf ("test SetPost err: %v" , doc )
33+ }
34+ fmt .Println (p .Title )
35+ fmt .Println (p .Body )
36+ }
37+
38+ func TestRfa (t * testing.T ) {
39+ p := PostFactory ("https://www.rfa.org/mandarin/Xinwen/6-07082020110802.html" )
40+ raw , doc , err := htmldoc .GetRawAndDoc (p .URL , 1 * time .Minute )
41+ if err != nil {
42+ t .Errorf ("GetRawAndDoc err: %v" , err )
43+ }
44+ p .Raw , p .DOC = raw , doc
45+ tc , err := Rfa (p )
46+ fmt .Println (tc )
47+ }
Original file line number Diff line number Diff line change @@ -79,6 +79,17 @@ func ExtractLinks(weburl string) ([]string, error) {
7979 return links , nil
8080}
8181
82+ func ElementsNext (doc * html.Node ) []* html.Node {
83+ nodes := []* html.Node {}
84+ visitNode := func (n * html.Node ) {
85+ if n .NextSibling != nil {
86+ nodes = append (nodes , n )
87+ }
88+ }
89+ ForEachNode (doc , visitNode , nil )
90+ return nodes
91+ }
92+
8293func ElementsByTagName (doc * html.Node , name ... string ) []* html.Node {
8394 var nodes []* html.Node
8495 if len (name ) == 0 {
You can’t perform that action at this time.
0 commit comments