Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jsoup提取多个div中的表_Java_Html_Jsoup - Fatal编程技术网

Java 使用Jsoup提取多个div中的表

Java 使用Jsoup提取多个div中的表,java,html,jsoup,Java,Html,Jsoup,我试图使用jsoup来访问嵌入在html页面多个div中的一个表。该表位于id为“content top”的外部分区下。我将给出指向该表的内部div:content top->center->middle right col->result 在div结果下;桌子是圆的。这是我想要访问的表,我需要遍历并打印其中包含的数据。下面是我一直尝试使用的java代码,但没有结果: Document doc = Jsoup.connect("http://www.calculator.com/#").data

我试图使用jsoup来访问嵌入在html页面多个div中的一个表。该表位于id为“content top”的外部分区下。我将给出指向该表的内部div:content top->center->middle right col->result

在div结果下;桌子是圆的。这是我想要访问的表,我需要遍历并打印其中包含的数据。下面是我一直尝试使用的java代码,但没有结果:

Document doc = Jsoup.connect("http://www.calculator.com/#").data("express", "sin(x)").data("calculate","submit").post();

// give the application time to calculate result before retrieving result from results table

try {                                  
Thread.sleep(10000); 
} 
catch(InterruptedException ex) 
{
Thread.currentThread().interrupt();
}

Elements content = doc.select("div#result") ;
Element tables = content.get(0) ;
Elements table_rows = tables.select("tr") ;
Iterator iterRows = table_rows.iterator();

while (iterRows.hasNext()) {

Element tr = (Element)iterRows.next();
Elements table_data = tr.select("td");
Iterator iterData = table_data.iterator();

int tdCount = 0;
String f_x_value = null;
String result = null;

// process new line
while (iterData.hasNext()) {

Element td = (Element)iterData.next();
switch (tdCount++) {
case 1:
f_x_value = td.text();
f_x_value = td.select("a").text();
break;

case 2:
result = td.text();
result = td.select("a").text();
break;          
}
}
System.out.println(f_x_value + "   " + result ) ;
} 

上面的代码崩溃了,几乎没有达到我想要的效果。任何人都可以帮助我

页面不会直接在div中为您提供id为“result”的表。它使用ajax类创建php文件并完成该过程。所以这里需要做的是首先构建一个类似json的

{"expression":"sin(x)","intVar":"x","upperBound":"","lowerBound":"","simplifyExpressions":false,"latex":"\\displaystyle\\int\\limits^{}_{}{\\sin\\left(x\\right)\\, \\mathrm{d}x}"}
expression
键保存要计算的表达式,
latex
是一个表达式,然后将其发布到
int.php
。这需要两个参数,即
q
,它是上面的json和
v
,它似乎是一个常量值
138019311
。我不明白这是什么

现在,这将返回如下响应

<html>
 <head></head>
 <body>
  <table class="round"> 
   <tbody>
    <tr class="">
     <th>$f(x) =$</th>
     <td>$\sin\left(x\right)$</td>
    </tr> 
    <tr class="sep odd">
     <th>$\displaystyle\int{f(x)}\, \mathrm{d}x =$</th>
     <td>$-\cos\left(x\right)$</td>
    </tr> 
   </tbody>
  </table> 
  <!-- Finished in 155 ms --> 
  <p id="share"> <img src="layout/32x32xshare.png.pagespeed.ic.i3iroHP5fI.png" width="32" height="32" /> <a id="share-link" href="http://www.integral-calculator.com/#expr=sin%28x%29" onclick="window.prompt(&quot;To copy this link to the clipboard, press Ctrl+C, Enter.&quot;, $(&quot;share-link&quot;).href); return false;">Direct link to this calculation (for sharing)</a> </p>
 </body>
</html>
根据评论进行更新。

unescape运行得非常好。在MathJax中,您可以右键单击并查看命令。因此,如果你去你的站点,尝试sin(x)方程,然后右键单击结果,然后像这样查看命令

您可以看到,这些命令正是我们在unescape之后得到的命令。演示站点没有呈现它。可能是演示站点的一个限制,仅此而已

public static String do_conversion (String str)
{
char c;
String output = "{";

for(int i = 0; i < str.length(); i++)
{
c = str.charAt(i);

if(c=='e')
output += "{mathrm{e}}";

else if(c=='(')
output += '{';

else if(c==')')
output += '}';

else if(c=='+')
output += "{cplus}";

else if(c=='-')
output += "{cminus}";

else if(c=='*')
output += "{cdot}";

else if(c=='/')
output += "{cdivide}";

else output += c; // else copy the character normally
}

output += ", mathrm{d}x}";
return output;
}

公共静态字符串do\u转换(字符串str)
{
字符c;
字符串输出=“{”;
对于(int i=0;i

@Syam S

请准确解释您看到的行为(“崩溃”没有帮助,完整堆栈跟踪是)以及您期望的行为。此外,Jsoup.connect().post()是一个阻塞调用--它将等待服务器的响应。没有理由在处理结果之前调用Thread.sleep。据我所知,该网站使用javascript显示结果。我非常确定jsoup在解析之前不会执行javascript。当没有执行javascript时,result div就是空的。我想你必须调查他们使用的ajax以获得结果。非常感谢@Syam S。你的贡献非常有用,而且很有效。我遇到了与你提供的解决方案类似的挑战。这一次,源网站是,我想写一个程序,通过JSOUP发送输入,例如sin(x),JSOUP将返回问题的结果。。我一直在尝试实现与您提供的逻辑类似的逻辑,但我没有正确实现它。请在这里也做同样的事情。这次它使用
diff.php
而不是
int.php
。因此,您应该是url和相同的参数q和v,比如
q:{“表达式”:“sin(x)”,“diffVar”:“x”,“diffOrder”:1,“simpleExpressions”:false,“showSteps”:false,“latex”:“\\dfrac{\\mathrm{d}}{\\mathrm{d}x}\\left(\\sin left(x\\right)\\ right)”)v:138019305
您可以使用chrome开发者工具来识别这些详细信息:)我将发布我访问衍生计算器网站的示例代码作为答案。我尝试过使用你在上面发布的相似性提示,但我得到一个错误,显示在返回的html中:对不起,出了点问题。。有人能帮我确定我的错误在哪里吗@Syam ST这可能是由于
v
的值发生了变化。我已经更新了积分和微分的答案。希望这有帮助。
public static String do_conversion (String str)
{
char c;
String output = "{";

for(int i = 0; i < str.length(); i++)
{
c = str.charAt(i);

if(c=='e')
output += "{mathrm{e}}";

else if(c=='(')
output += '{';

else if(c==')')
output += '}';

else if(c=='+')
output += "{cplus}";

else if(c=='-')
output += "{cminus}";

else if(c=='*')
output += "{cdot}";

else if(c=='/')
output += "{cdivide}";

else output += c; // else copy the character normally
}

output += ", mathrm{d}x}";
return output;
}