java - 无法从网页读取 html

标签 java jsoup

我试图从网页获取一些数据(html 标签),但我不能。由于某种原因,我只得到主要是空的标签。

这是网址:http://www.miamidade.gov/transit/mobile/routes.asp

这是我的java代码:

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
class xyz{
    public static void main (String[] args) throws IOException {
        Document doc =  jsoup.connect("http://www.miamidade.gov/transit/mobile/routes.asp").userAgent(" Mozilla/5.0").timeout(3000).post();
        String title = doc.html();
        System.out.print(title);
    }
}

最佳答案

页面http://www.miamidade.gov/transit/mobile/routes.asp首先,JavaScript 重定向到“scriptCheck.asp?script=yes&CurrentPage=/transit/mobile/routes.asp?” 。然后它最终重新加载http://www.miamidade.gov/transit/mobile/routes.asp再次使用您在页面上看到的信息。 Jsoup 似乎无法处理该重定向,因此您的代码获取第一页并返回该 HTML,该 HTML 与您使用浏览器时看到的 HTML 不同。也许这就是您找不到所需信息的原因。

第一页源代码

<html>
 <head> 
  <title></title> 
  <script language="JavaScript">
<!--
window.location="scriptCheck.asp?script=yes&CurrentPage=/transit/mobile/routes.asp?";
//-->

  </script>
 </head>  
 <body>
  <noscript> 
   <meta http-equiv="Refresh" content="0;URL=scriptCheck.asp?script=no&amp;CurrentPage=/transit/mobile/routes.asp?" /> 
  </noscript>  
  <noscript> 
   <br /> 
   <br /> 
   <a href="scriptCheck.asp?script=no&amp;CurrentPage=/transit/mobile/routes.asp?">Enter MDT Mobile Services Site</a> 
   <br /> 
   <br /> 
  </noscript>   
 </body>
</html>

关于java - 无法从网页读取 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6648326/

相关文章:

java - Retrofit2 状态码为 200 但 json 结构不同于数据模型类时的处理条件

java - 通过不同的方法写入文件

java - 如何使用 JSOUP java 获取谷歌搜索的第一个标题和网址?

java - 如何使用JSoup获取超链接href?

java - 在 Java 中以编程方式添加 <p> 标签

java - Jsoup:在不同类之间选择 HTML

java - 如果 edittext 包含该词,如何为部分 android 文本着色

java - 你的 ShouldNeverHappenException 是什么?

java - java中的导出和导入函数

android - Jsoup,无法检索对象