【问题标题】:JSoup request url changesJSoup 请求 url 更改
【发布时间】:2016-07-27 04:19:12
【问题描述】:

我正在尝试使用 JSoup 访问 java 中的在线 api。 通过我的浏览器,我可以很好地导航到 url,然后我得到 json 数据作为回报。但是,如果我使用 Jsoup,则 url 会被更改,并且 '/api' 会从中删除。 例如:https://www.onehash.com/api/archived_contest/122/ 我可以通过浏览器很好地打开它,但是如果我使用 jsoup 连接到它,则 url 变为 https://www.onehash.com/archived_contest/122/ 并返回 404 错误。 (插入空间,因为不能发布超过 2 个链接) 概述 url:https://www.onehash.com/api/disciplines_json/ 我可以用 Jsoup 连接到就好了...

有人知道这会发生什么吗?我尝试在标头中发送用户代理,但无济于事。

我用来连接的代码是:

Jsoup.connect("https://www.onehash.com/api/archived_contest/122")
     .userAgent("Mozilla")
     .ignoreContentType(true)
     .get();

抛出一个HttpStatusException

【问题讨论】:

  • 我用一些代码编辑了帖子

标签: java web-scraping jsoup


【解决方案1】:

用斜线结尾就可以了:

//                                                             v--- Slash here
Jsoup.connect("https://www.onehash.com/api/archived_contest/122/")
     .userAgent("Mozilla")
     .ignoreContentType(true)
     .get();

它还在浏览器中返回不带斜杠的 404。

【讨论】:

  • 非常感谢!我怎么没注意到这个T.T
猜你喜欢
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 2011-07-28
相关资源
最近更新 更多