代码之家  ›  专栏  ›  技术社区  ›  tamtom

使用JSoup抓取Google天气结果

  •  0
  • tamtom  · 技术社区  · 11 年前

    我正在尝试从谷歌网页获取天气状况,HTML元素上有一个标记 wob_tm ,我试图获取值,但总是得到一个例外

     try {
          Document  document = Jsoup.connect("https://www.google.jo/webhp?hl=en#hl=en-JO&q=amman+weather").get();
          Element temp = document.getElementById("wob_tm");
          System.out.println(temp.text()); // Exception in thread "main" java.lang.NullPointerException
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    

    enter image description here

    1 回复  |  直到 11 年前
        1
  •  1
  •   tamtom    11 年前

    查询中出现错误,我应该定义一个用户代理

    Document doc = Jsoup
        .connect("https://www.google.com/search?q=mario");
        .userAgent("Mozilla/5.0")
        .timeout(5000).get();