【问题标题】:JSoup.connect throws 403 error while apache.httpclient is able to fetch the contentJSoup.connect 抛出 403 错误,而 apache.httpclient 能够获取内容
【发布时间】:2012-04-24 15:18:58
【问题描述】:

我正在尝试解析任何给定页面的 HTML 转储。我使用了HTML Parser 并尝试了JSoup 进行解析。

我在 Jsoup 中找到了有用的函数,但在调用 Document doc = Jsoup.connect(url).get(); 时出现 403 错误

我尝试了 HTTPClient,以获取 html 转储,并且对于相同的 url 是成功的。

为什么 JSoup 为从 commons http 客户端提供内容的同一 URL 提供 403? 难道我做错了什么?有什么想法吗?

【问题讨论】:

  • 请分享链接。你连接到。
  • 查看链接stackoverflow.com/questions/7508813/…,有类似问题的报告
  • 我会尝试使用 tcpdump 或类似工具来查看 HTTP 客户端发送的数据和 JSoup 发送的数据的差异。
  • @vacuum google.com/…
  • @AkashYadav 谢谢!用户代理的事情很有意义并且有效!

标签: java html-parsing jsoup


【解决方案1】:

工作解决方案如下(感谢 Angelo Neuschitzer 提醒将其作为解决方案):

Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
Elements links = doc.getElementsByTag(HTML.Tag.CITE.toString);
for (Element link : links) {
            String linkText = link.text();
            System.out.println(linkText);
}

所以,userAgent 成功了 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多