JSoup查询:如何在另一个标记中查找HTML标记的所有实例(JSoup query: how to find all instances of an HTML tag in another tag)
我有一个HTML页面来扫描标题和图形元素。 HTML文件的代码是:
<body> <section> <h2>H2-1</h2> <div> <section> <h3>H3-1</h3> <div> <figure><img src="Fig 1.png"></figure> </div> </section> <section> <h3>H3-2</h3> <div></div> </section> </div> </section> <section> <h2>H2-2</h2> <div> <figure><img src="Fig 2.png"></figure> <figure><img src="Fig 3.png"></figure> <figure><img src="Fig 4.png"></figure> </div> </section> <section> <h2>H2-3</h2> <div> <figure><img src="Fig 5.png"></figure> <section> <h3>H3-3</h3> <div><figure><img src="Fig 6.png"></figure></div> </section> </div> </section> <section> <h2>H2-4</h2> <div> </div> </section> </body>
我想要的输出是:
H2-1 H3-1 Fig 1 H2-2 Fig 2 Fig 3 Fig 4 H2-3 Fig 5 H3-3 Fig 6
我想找到显示所有具有子图元素的标题。 如果它具有直接子图形元素,我不想显示标题。
我正在尝试的JSoup代码是:
Document doc = Jsoup.parse(sourceCode); Elements sectionTags = doc.body().getElementsByTag("section"); for (Element sectTag : sectionTags) { System.out.println (sectTag.children().first().ownText()); //print the Heading text Elements figureTags = sectTag.getElementsByTag("figure"); for (Element figTag : figureTags) { System.out.println (figTag.getElementsByTag("img").attr("src").toString()); // print the image name } }
但我没有得到理想的输出。 我得到的输出是:
H2-1 Fig 1 H3-1 Fig 1 H2-2 Fig 2 H2-3 Fig 5 H3-3 Fig 6
有帮助吗? 我是JSoup的新手,感谢任何有用的建议或提示。
提前致谢。
I have an HTML page to scan for headings and figure elements. The code of the HTML file is:
<body> <section> <h2>H2-1</h2> <div> <section> <h3>H3-1</h3> <div> <figure><img src="Fig 1.png"></figure> </div> </section> <section> <h3>H3-2</h3> <div></div> </section> </div> </section> <section> <h2>H2-2</h2> <div> <figure><img src="Fig 2.png"></figure> <figure><img src="Fig 3.png"></figure> <figure><img src="Fig 4.png"></figure> </div> </section> <section> <h2>H2-3</h2> <div> <figure><img src="Fig 5.png"></figure> <section> <h3>H3-3</h3> <div><figure><img src="Fig 6.png"></figure></div> </section> </div> </section> <section> <h2>H2-4</h2> <div> </div> </section> </body>
The output I want is:
H2-1 H3-1 Fig 1 H2-2 Fig 2 Fig 3 Fig 4 H2-3 Fig 5 H3-3 Fig 6
I want to find show all headings that has a child figure element. I do not want to display the heading if it does to have an immediate child figure element.
The JSoup code I am trying is:
Document doc = Jsoup.parse(sourceCode); Elements sectionTags = doc.body().getElementsByTag("section"); for (Element sectTag : sectionTags) { System.out.println (sectTag.children().first().ownText()); //print the Heading text Elements figureTags = sectTag.getElementsByTag("figure"); for (Element figTag : figureTags) { System.out.println (figTag.getElementsByTag("img").attr("src").toString()); // print the image name } }
but I am not getting the desired output. The output that I get is:
H2-1 Fig 1 H3-1 Fig 1 H2-2 Fig 2 H2-3 Fig 5 H3-3 Fig 6
Any help? I am new to JSoup and appreciate any suggestion or tip that can work.
Thanks in advance.
原文:https://stackoverflow.com/questions/37682061
最满意答案
在这两种情况下你都做错了:
malloc
和calloc
。
malloc
将分配您告诉它的字节数。你调用
malloc(sizeof(member_size))
:
变量member_size
类型为size_t
(或多或少为int
)。
sizeof(size_t)
是4个字节(在许多平台上)。
因此,sizeof(member_size)
是4个字节,您正在调用malloc(4)
。我认为你不想要4个字节。
我想你想要malloc(nmember * member_size)
同样,对于calloc,你想要:calloc(nmember, member_size)
我不知道为什么你没有充分理由抛出随机
sizeof
电话。You are doing it wrong in both cases:
malloc
andcalloc
.
malloc
will allocate the number of bytes you tell it to.You call
malloc(sizeof(member_size))
:
Variablemember_size
has typesize_t
(more-or-less anint
).
sizeof(size_t)
is 4-bytes (on many platforms).
So,sizeof(member_size)
is 4-bytes, and you are callingmalloc(4)
.I don't think you want 4 bytes.
I think you wantmalloc(nmember * member_size)
Similarly, for calloc, you want:calloc(nmember, member_size)
I don't know why you're throwing in random
sizeof
calls for no good reason.
相关问答
更多-
List * List_createNode(const char * str){ List * listPointer = (List *) malloc(sizeof(List)); if(listPointer == NULL) return NULL; listPointer->next = NULL; listPointer->str = strdup(str); if(listPointer->str == NULL){ free(listPointer ...
-
如何继续使用malloc?(how to keep using malloc?)[2022-08-21]
而不是使用malloc ,使用realloc 。 不要在while循环中使用feof(in) 。 看看为什么 。 int number; while( fscanf(in, "%d", &number) == 1 ){ a = realloc(a, sizeof(int)*(size+1)); if ( a == NULL ) { // Problem. exit(0); } a[size] = number; printf("a[ ... -
正确使用malloc(Using malloc correctly)[2023-06-30]
在这两种情况下你都做错了: malloc和calloc 。 malloc将分配您告诉它的字节数。 你调用malloc(sizeof(member_size)) : 变量member_size类型为size_t (或多或少为int )。 sizeof(size_t)是4个字节(在许多平台上)。 因此, sizeof(member_size)是4个字节,您正在调用malloc(4) 。 我认为你不想要4个字节。 我想你想要malloc(nmember * member_size) 同样,对于calloc,你想要 ... -
students *mynew= realloc(s, sizeof(students)* (scount+1)); if( mynew != NULL ) s=mynew; Otehrwise你有内存泄漏。 您没有使用realloc的返回值。 不要malloc类型。 按照标准§7.22.2.35 void *realloc(void *ptr, size_t size) realloc函数解除分配ptr指向的旧对象,并返回指向具有size指定size的新对象的指针。 最好不要使用调用mallo ...
-
pack->data = &buffer; 执行该行后, data将保存变量buffer的地址。 它将不再保存它以前保存的堆的地址(如果你没有把它放在别的地方,那么这个地址现在就被泄露了)。 然而, free只能用于堆上的地址,所以在pack->data调用free调用未定义的行为。 pack->data = &buffer; After executing that line, data will hold the address of the variable buffer. It will no ...
-
Malloc和scanf(Malloc and scanf)[2021-12-06]
char *toParseStr = (char*)malloc(10); printf("Enter string here: "); scanf("%s",toParseStr); printf("%s",toParseStr); free(toParseStr); 首先, scanf的字符串指定它将要接收的输入。 为了在接受键盘输入之前显示一个字符串,使用printf ,如图所示。 其次,由于您使用malloc分配的指向10号字符数组,因此您不需要解除引用到toParseStr 。 ... -
两者都很好。 唯一的区别是如果您尝试第二次释放myPtr ,则前一种方法会崩溃。 根据您使用的语言, malloc行可能会被整理一些。 当你稍后重构时,使用sizeof(*myPtr)不太容易出现错误。 如果你使用C,演员阵容也是不必要的 double* myPtr = malloc(sizeof(*myPtr)*5); 正如WhozCraig所指出的那样,如果您使用的是C ++,则分配数组的方法会简单得多 std::vector
ar(5); 给你一个5个double精度数组,如果需 ... -
做检查是正确的,有些人会说迂腐。 一些想法 #include
... // MAX_CHAIN > SIZE_MAX/sizeof(char*) MAX_CHAIN > (SIZE_MAX - sizeof(char*) +1)/sizeof(char*) // This deals with SIZE_MAX/sizeof(char*) rounding toward 0 如果内存分配大小为0,则返回值为NULL。 监视尝试的负面内存分配请求,因为尽管请求需要size_t (无符号 ... -
正确的方法来计算malloc()和realloc()的大小?(Correct way to calculate the size for malloc() and realloc()?)[2023-11-02]
他们是一样的。 使用前一个示例的原因是,如果您决定更改类型,以便以后简化维护。 也就是说,不要像那样使用realloc - 如果失败,你将泄漏原始的randomNum分配。 They're the same. The reason to use your former example is to ease maintenance later if you decide to change types. That said, don't use realloc like that - if it fails, ... -
正确使用malloc(Using malloc properly)[2023-08-09]
在 getcwd(dir, sizeof(*dir)) sizeof生成1,因为*dir指的是单个char 。 这不是你想要的。 如果用512替换它,那么一切都应该正常工作。 惯用编码将是这样的: int main(int ac, char **av) { const int buf_size = 512; char *dir; if(ac > 2) { dir = malloc(buf_size); getcwd(dir, buf_size); ...