質問編集履歴

1

ソースコード追加

2016/01/23 08:23

投稿

J.Spei
J.Spei

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,32 +1,140 @@
1
1
  HTMLページの<p><p></p></p>で囲まれた文章をC++で表示したいのですが、うまくいきません。
2
2
 
3
- なに怪異方法はありませんか??
3
+ なにか良い方法はありませんか??
4
4
 
5
- 現在こんな感じのプログラムでやってます(一部)
5
+ 現在こんな感じのプログラムでやってます
6
6
 
7
7
 
8
8
 
9
9
  ---------------------------------------------------
10
10
 
11
- //<p><p>
11
+ #include <libxml/HTMLparser.h>
12
12
 
13
- if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
13
+ #include <iostream>
14
14
 
15
- for(xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next){
15
+ #include <string>
16
16
 
17
- if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
17
+ #include <fstream>
18
18
 
19
- count = (char *)node->next->next->children->content;
19
+ #include <stdio.h>
20
20
 
21
- string a = count;
21
+ #include <string.h>
22
22
 
23
- printf("%s\n",count);
23
+ #include <stdlib.h>
24
24
 
25
- }
26
25
 
26
+
27
+ using namespace std;
28
+
29
+
30
+
31
+ void FindInfo(xmlNode*& element);
32
+
33
+
34
+
35
+ int main() {
36
+
37
+ //HTML用の構造体
38
+
39
+ htmlDocPtr m_doc;
40
+
41
+
42
+
43
+ // ファイル名とエンコードの設定
44
+
45
+ char* file = "http://www.visit-hokkaido.jp/article/detail/2";
46
+
47
+ char* enc = "utf-8";
48
+
49
+ // xmlの読み込み
50
+
51
+ if (m_doc = htmlReadFile(file, enc, HTML_PARSE_RECOVER)) {
52
+
53
+ htmlNodePtr root = xmlDocGetRootElement(m_doc);
54
+
55
+ if (root != NULL) {
56
+
57
+ FindInfo(root);
58
+
27
- }
59
+ }
60
+
61
+ xmlFreeDoc(m_doc);
62
+
63
+ m_doc = NULL;
64
+
65
+ }
66
+
67
+
68
+
69
+ xmlCleanupParser();
70
+
71
+ xmlCleanupCharEncodingHandlers();
72
+
73
+ return;
28
74
 
29
75
  }
76
+
77
+
78
+
79
+ //地域
80
+
81
+ void FindInfo(xmlNode*& element){
82
+
83
+ char* count;
84
+
85
+ for (htmlNodePtr node = element; node != NULL; node = node->next){
86
+
87
+ //<p><p>
88
+
89
+ count = (char *)node->next->next->children->content;
90
+
91
+ string a = count;
92
+
93
+ printf("%s\n",count);
94
+
95
+ if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
96
+
97
+ for(xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next){
98
+
99
+ if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
100
+
101
+ count = (char *)node->next->next->children->content;
102
+
103
+ string a = count;
104
+
105
+ printf("%s\n",count);
106
+
107
+ }
108
+
109
+ }
110
+
111
+ }
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+ //再起読み込み
124
+
125
+ if(node->children != NULL)
126
+
127
+ {
128
+
129
+ FindInfo(node->children);
130
+
131
+ }
132
+
133
+ }
134
+
135
+ }
136
+
137
+
30
138
 
31
139
 
32
140