java程序示例

Sometime back I was looking for a way to search Google using Java Program. I was surprised to see that Google had a web search API but it has been deprecated long back and now there is no standard way to achieve this.

有时,我正在寻找一种使用Java程序搜索Google的方法。 我很惊讶地看到Google拥有一个Web搜索API,但是很早以前就已弃用了它,现在没有标准的方法可以实现此目的。

Basically google search is an HTTP GET request where query parameter is part of the URL, and earlier we have seen that there are different options such as Java HttpUrlConnection or Apache HttpClient to perform this search. But the problem is more related to parsing the HTML response and get the useful information out of it. That’s why I chose to use jsoup that is an open source HTML parser and it’s capable to fetch HTML from given URL.

基本上,谷歌搜索是一个HTTP GET请求,其中查询参数是URL的一部分,并且我们之前已经看到有不同的选项(例如Java HttpUrlConnection或Apache HttpClient)来执行此搜索。 但是问题更多与解析HTML响应并从中获取有用信息有关。 这就是为什么我选择使用jsoup ,它是一个开放源代码HTML解析器,并且能够从给定的URL中获取HTML。

So below is a simple program to fetch google search results in a java program and then parse it to find out the search results.

因此,以下是一个简单的程序,可通过Java程序获取Google搜索结果,然后对其进行解析以找出搜索结果。

package com.journaldev.jsoup;import java.io.IOException;
import java.util.Scanner;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;public class GoogleSearchJava {public static final String GOOGLE_SEARCH_URL = "https://www.google.com/search";public static void main(String[] args) throws IOException {//Taking search term input from consoleScanner scanner = new Scanner(System.in);System.out.println("Please enter the search term.");String searchTerm = scanner.nextLine();System.out.println("Please enter the number of results. Example: 5 10 20");int num = scanner.nextInt();scanner.close();String searchURL = GOOGLE_SEARCH_URL + "?q="+searchTerm+"&num="+num;//without proper User-Agent, we will get 403 errorDocument doc = Jsoup.connect(searchURL).userAgent("Mozilla/5.0").get();//below will print HTML data, save it to a file and open in browser to compare//System.out.println(doc.html());//If google search results HTML change the <h3 class="r" to <h3 class="r1"//we need to change below accordinglyElements results = doc.select("h3.r > a");for (Element result : results) {String linkHref = result.attr("href");String linkText = result.text();System.out.println("Text::" + linkText + ", URL::" + linkHref.substring(6, linkHref.indexOf("&")));}}}

Below is a sample output from above program, I saved the HTML data into file and opened in a browser to confirm the output and it’s what we wanted. Compare the output with below image.

下面是上述程序的输出示例,我将HTML数据保存到文件中,并在浏览器中打开以确认输出,这就是我们想要的。 将输出与下图进行比较。

Please enter the search term.
journaldev
Please enter the number of results. Example: 5 10 20
20
Text::JournalDev, URL::=https://www.journaldev.com/
Text::Java Interview Questions, URL::=https://www.journaldev.com/java-interview-questions
Text::Java design patterns, URL::=https://www.journaldev.com/tag/java-design-patterns
Text::Tutorials, URL::=https://www.journaldev.com/tutorials
Text::Java servlet, URL::=https://www.journaldev.com/tag/java-servlet
Text::Spring Framework Tutorial ..., URL::=https://www.journaldev.com/2888/spring-tutorial-spring-core-tutorial
Text::Java Design Patterns PDF ..., URL::=https://www.journaldev.com/6308/java-design-patterns-pdf-ebook-free-download-130-pages
Text::Pankaj Kumar (@JournalDev) | Twitter, URL::=https://twitter.com/journaldev
Text::JournalDev | Facebook, URL::=https://www.facebook.com/JournalDev
Text::JournalDev - Chrome Web Store - Google, URL::=https://chrome.google.com/webstore/detail/journaldev/ckdhakodkbphniaehlpackbmhbgfmekf
Text::Debian -- Details of package libsystemd-journal-dev in wheezy, URL::=https://packages.debian.org/wheezy/libsystemd-journal-dev
Text::Debian -- Details of package libsystemd-journal-dev in wheezy ..., URL::=https://packages.debian.org/wheezy-backports/libsystemd-journal-dev
Text::Debian -- Details of package libsystemd-journal-dev in sid, URL::=https://packages.debian.org/sid/libsystemd-journal-dev
Text::Debian -- Details of package libsystemd-journal-dev in jessie, URL::=https://packages.debian.org/jessie/libsystemd-journal-dev
Text::Ubuntu – Details of package libsystemd-journal-dev in trusty, URL::=https://packages.ubuntu.com/trusty/libsystemd-journal-dev
Text::libsystemd-journal-dev : Utopic (14.10) : Ubuntu - Launchpad, URL::=https://launchpad.net/ubuntu/utopic/%2Bpackage/libsystemd-journal-dev
Text::Debian -- Details of package libghc-libsystemd-journal-dev in jessie, URL::=https://packages.debian.org/jessie/libghc-libsystemd-journal-dev
Text::Advertise on JournalDev | BuySellAds, URL::=https://buysellads.com/buy/detail/231824
Text::JournalDev | LinkedIn, URL::=https://www.linkedin.com/groups/JournalDev-6748558
Text::How to install libsystemd-journal-dev package in Ubuntu Trusty, URL::=https://www.howtoinstall.co/en/ubuntu/trusty/main/libsystemd-journal-dev/
Text::[global] auth supported = cephx ms bind ipv6 = true [mon] mon data ..., URL::=https://zooi.widodh.nl/ceph/ceph.conf
Text::UbuntuUpdates - Package "libsystemd-journal-dev" (trusty 14.04), URL::=https://www.ubuntuupdates.org/libsystemd-journal-dev
Text::[Journal]Dev'err - Cursus Honorum - Enjin, URL::=https://cursushonorum.enjin.com/holonet/m/23958869/viewthread/13220130-journaldeverr/post/last

That’s all for google search in a java program, use it cautiously because if there is unusual traffic from your computer, chances are Google will block you.

这就是在Java程序中进行Google搜索的全部内容,请谨慎使用,因为如果您的计算机出现异常流量,则Google可能会阻止您。

翻译自: https://www.journaldev.com/7207/google-search-from-java-program-example

java程序示例

java程序示例_Java程序中的Google搜索示例相关推荐

  1. 借助 ONLYOFFICE 宏在电子表格中插入 Google 搜索结果

    网络搜索已经成为我们生活之中必不可缺的一个部分,甚至已经成为了我们的日常生活.但是,如果我们想将搜索结果存储于别处,该怎么办呢?比如存储在电子表格中-这是一种非常便捷的数据操作方式.在本文中,我们将展 ...

  2. 在Android应用中实现Google搜索的例子

    有一个很简单的方法在你的 Android 应用中实现 Google 搜索.在这个例子中,我们将接受用户的输入作为搜索词,我们将使用到 Intent.ACTION_WEB_SEARCH . Google ...

  3. java程序结构_Java 程序结构说明(学习 Java 编程语言 004)

    1. 一个简单的类 创建 FirstSample.java 文件,来编写最简单的 Java 应用程序.下面是示例代码: public class FirstSample { public static ...

  4. java程序设计封面_Java程序实例封面

    本教程是为Java的初学者和专业人士而设计的,提供有关Java的基本和高级概念中的相关程序示例,方便大家快速入门学习和参考. 在这里可找到实用的Java编程示例.Java可以在各种平台上运行,如Win ...

  5. Java 匿名存储过程_Java程序员的存储过程

    存储过程是指保存在数据库并在数据库端执行的程序.你可以使用特殊的语法在Java类中调用存储过程.在调用时,存储过程的名称及指定的参数通过JDBC连接发送给DBMS,执行存储过程并通过连接(如果有)返回 ...

  6. java程序结构_java程序结构

    java是一门面向对象的语言,在编程过程中当然离不开对象的声明,而对象又是通过类定义的,所以java中最重要的就是各式各样的类,在java中,类也是一个程序的基本单位 0x01:默认生成类 在ecli ...

  7. java自动封箱_Java程序员面试,自动封箱/拆箱原理与包装类的缓冲机制你知道么?(转)...

    概述 本文中小编为大家细致的讲解了Java中基本数据类型对应的包装类以及包装类的缓冲机制在实际开发中的应用 . 并且对Java中基本数据类型的包装类的主要应用---自动封箱.自动拆箱做了底层剖析 . ...

  8. java程序组成_java程序是由什么组成的

    展开全部 1.源程序文32313133353236313431303231363533e78988e69d8331333363373732件的构成 要编写Java程序,首先应该知道Java程序文件中必 ...

  9. java整除输出_Java程序输出可被其他数字整除的数字

    我有一个程序,该程序读取两个实数,然后打印出这两个之间的所有数字,这些数字可以被2或3或5整除.该程序可以正常工作,但是当用户输入两个非常大的数字时(例如1122222123333)和21412332 ...

最新文章

  1. 学术青年如何克服拖延症——5条技巧助你前进
  2. 选择排序-直接选择排序
  3. golang中的strings.ToTitle
  4. (轉貼) Embedded System與System on Chip的差異 (IC Design)
  5. 北斗导航 | 基于CRDSS(Comprehensive RDSS, 全面RDSS)的北斗抗干扰技术研究:RDSS+RNSS(论文翻译)
  6. react-native多图选择、图片裁剪(支持ad/ios图片个数控制)
  7. 插件开发 之 生成代码
  8. 10个开源且优秀的后台管理系统UI面板
  9. Linux内核开发:创建proc文件并与用户空间接口
  10. [论文阅读] Boosting Salient Object Detection with Transformer-based Asymmetric Bilateral U-Net
  11. .NET 基础 一步步 一幕幕 [前言]
  12. 【大数据入门二——yarn和mapreduce】
  13. 【以前的空间】网络流合集
  14. Gliffy Diagrams 安装问题
  15. 战略分析思路——沙盘推演逻辑
  16. Eclipse版本代号
  17. 单面打印机=》双面打印
  18. 聚合查询越来越慢?——详解Elasticsearch的Global Ordinals与High Cardinality
  19. Python中通过property实现属性的修改、删除、查看
  20. android 录像限制时间,视频拍摄能不能限制拍摄时间和微信小视频一样

热门文章

  1. 如何理解 RxJS?RxJS的中文API和使用教程
  2. Ubuntu 14.04卸载安装失败的Mysql数据库,以及重新安装配置
  3. AIR学习教程(一)
  4. osmand中矢量数据地图绘制
  5. 温故而知新:new与override的差异以及virtual方法与abstract方法的区别
  6. [转载] java 捕获异常还是抛出异常
  7. [转载] comma.ai自动驾驶代码浅析及实践
  8. [转载] Python数据分析与可视化学习笔记(一)数据分析与可视化概述
  9. FPGA中数的表示方法
  10. 优化MyBatis配置文件中的配置