我一直在尝试从 nytimes.com 获取一些数据,但不幸的是,所有有趣的部分都丢失了。我正在尝试获取给定输入查询的搜索结果。当我通过 postman 或 php 中的 file_get_contents 执行此操作时,结果是相同的 - 我没有得到 resultSearch 部分。我读到我可能需要一些 cookie 或授权,但没有任何帮助。有任何想法吗? PS我尝试了很多变体 - 有或没有一些选项,例如:followRedirects
String serachPhraze = "africa flood death";
try
{
Connection.Response doc = Jsoup.connect("http://query.nytimes.com/search/sitesearch/#/" + serachPhraze.replaceAll("\\s+","+"))
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.referrer("http://www.google.com")
.timeout(12000)
.followRedirects(true)
.execute();
最佳答案
结果通过ajax获取并通过javascript动态生成。 Jsoup 无法处理这个问题。您从纯粹的解析器中获得的内容就是您按下 Ctrl+U(在 Chrome 中)时看到的内容。这是服务器生成的 html,也是您通过 jsoup 或 file_get_contents(在 php 中)获得的唯一内容。如果你想要 javascript 内容,那么你必须使用像 selenium 这样包含 javascript 引擎的东西。 Selenium 将运行 javascript 代码,然后获取内容。
关于java - jsoup 获取 HTML 失败(缺少某些部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926761/