首页 \ 问答 \ JSoup查询:如何在另一个标记中查找HTML标记的所有实例(JSoup query: how to find all instances of an HTML tag in another tag)

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
更新时间:2023-11-01 06:11

最满意答案

在这两种情况下你都做错了: malloccalloc

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 and calloc.

malloc will allocate the number of bytes you tell it to.

You call malloc(sizeof(member_size)):
Variable member_size has type size_t (more-or-less an int).
sizeof(size_t) is 4-bytes (on many platforms).
So, sizeof(member_size) is 4-bytes, and you are calling malloc(4).

I don't think you want 4 bytes.
I think you want malloc(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 ,使用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和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 ...
  • 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 (无符号 ...
  • 他们是一样的。 使用前一个示例的原因是,如果您决定更改类型,以便以后简化维护。 也就是说,不要像那样使用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, ...
  • 在 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); ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)