teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースコード追加

2016/01/23 08:23

投稿

J.Spei
J.Spei

スコア10

title CHANGED
File without changes
body CHANGED
@@ -1,19 +1,73 @@
1
1
  HTMLページの<p><p></p></p>で囲まれた文章をC++で表示したいのですが、うまくいきません。
2
- なに怪異方法はありませんか??
2
+ なにか良い方法はありませんか??
3
- 現在こんな感じのプログラムでやってます(一部)
3
+ 現在こんな感じのプログラムでやってます
4
4
 
5
5
  ---------------------------------------------------
6
+ #include <libxml/HTMLparser.h>
7
+ #include <iostream>
8
+ #include <string>
9
+ #include <fstream>
10
+ #include <stdio.h>
11
+ #include <string.h>
12
+ #include <stdlib.h>
13
+
14
+ using namespace std;
15
+
16
+ void FindInfo(xmlNode*& element);
17
+
18
+ int main() {
19
+ //HTML用の構造体
20
+ htmlDocPtr m_doc;
21
+
6
- //<p><p>
22
+ // ファイル名とエンコードの設定
7
- if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
8
- for(xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next){
23
+ char* file = "http://www.visit-hokkaido.jp/article/detail/2";
9
- if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
10
- count = (char *)node->next->next->children->content;
11
- string a = count;
12
- printf("%s\n",count);
24
+ char* enc = "utf-8";
25
+ // xmlの読み込み
26
+ if (m_doc = htmlReadFile(file, enc, HTML_PARSE_RECOVER)) {
27
+ htmlNodePtr root = xmlDocGetRootElement(m_doc);
28
+ if (root != NULL) {
29
+ FindInfo(root);
13
- }
30
+ }
31
+ xmlFreeDoc(m_doc);
32
+ m_doc = NULL;
14
- }
33
+ }
34
+
35
+ xmlCleanupParser();
36
+ xmlCleanupCharEncodingHandlers();
37
+ return;
15
38
  }
16
39
 
40
+ //地域
41
+ void FindInfo(xmlNode*& element){
42
+ char* count;
43
+ for (htmlNodePtr node = element; node != NULL; node = node->next){
44
+ //<p><p>
45
+ count = (char *)node->next->next->children->content;
46
+ string a = count;
47
+ printf("%s\n",count);
48
+ if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
49
+ for(xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next){
50
+ if(xmlStrcasecmp(node->name, (const xmlChar*)"p") == 0){
51
+ count = (char *)node->next->next->children->content;
52
+ string a = count;
53
+ printf("%s\n",count);
54
+ }
55
+ }
56
+ }
57
+
58
+
59
+
60
+
61
+
62
+ //再起読み込み
63
+ if(node->children != NULL)
64
+ {
65
+ FindInfo(node->children);
66
+ }
67
+ }
68
+ }
69
+
70
+
17
71
  ---------------------------------------------------
18
72
  <!-- /inner-->
19
73
  <div class="inner">