Java - 查询Google

时间:2015-11-16 15:16:27

标签: java http search

我尝试使用小型UI在Java中编写一个小程序代码,让您使用一些谷歌搜索的关键字来改善您的搜索。

我有2个文本字段(一个用于网站,一个用于关键字)和2个日期选择器,让用户选择搜索结果的日期范围。

当我按下搜索按钮时,它将连接到以下网址:

"https://www.google.it/search?q=" + site + Keywords + daterange 
  • site =" site:SITE_MAIN_URL"
  • 关键字是我要找的关键字
  • daterange =" daterange:JULIAN_DATE_1 - JULIAN_DATE_2"

毕竟我得到了前10个结果,但问题就在这里......

如果我没有选择日期,我可以轻松获取链接

如果我设置日期范围,我会收到HTTP 503错误,即服务不可用的错误(如果我在网络浏览器上粘贴生成的网址,一切正常)

(用户代理设置为mozilla 5.0)

编辑:没有发布任何代码:P

//here i generate the site
site = "site:" + website_field.getText();

//here i convert the dates using a class found on the net
d1 = (int) DateLabelFormatter.dateToJulian(date1);
d2 = (int) DateLabelFormatter.dateToJulian(date2);
daterange += "+daterange:" + d1 + "-" + d2;

//here i generate the keywords
keywords = keyword_field.getText();
String[] keyword = keywords.split(" ");
for (int i = 0; i < keyword.length; i++) {
                        tempKeyword += "+" + keyword[i];
                    }

//the query
query = "https://www.google.it/search?q=" + site + tempKeyword + daterange;

//the connection (wrapped in a try-catch)
Document jSoupDoc = Jsoup.connect(query).userAgent("Mozilla/5.0").timeout(5000).get();


//fetching the links
Elements links = jSoupDoc.select("a[href]");
Element link;
for (int i = 0; i < links.size(); i++) {

    link = links.get(i);
    String temp = link.attr("href");

    // filtering the first 10 google links
    if (temp.contains("url")) //donothing
        if (temp.contains("webcache")) { //donothing
        } else {
            String[] splitTemp = temp.split("=");
            String[] splitTemp2 = splitTemp[1].split("&sa");
            System.out.println(splitTemp2[0]);
            }
        }

如果我没有选择日期,那么在执行所有这些(NotSoWellWritten)代码之后,我只使用&#34; site&#34;和#34;关键字&#34;我可以在控制台上看到谷歌搜索页面上找到的前10个结果。 如果我从datepickers中选择日期范围,我会得到503错误。

如果您想尝试一个有效的查询,可以在facebook.com上搜索关键字&#34; dog&#34;从11月1日开始到15日用这个&#34;工具&#34;

生成

https://www.google.it/search?q=site:facebook.com+dog+daterange:2457328-2457342

`

2 个答案:

答案 0 :(得分:0)

使用以下代码时没有问题:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        // the connection (wrapped in a try-catch)
        Document jSoupDoc = Jsoup.connect("https://www.google.it/search?q=site:facebook.com+dog+daterange:2457328-2457342").userAgent("Mozilla/5.0").timeout(5000).get();

        // fetching the links
        Elements links = jSoupDoc.select("a[href]");
        Element link;
        for (int i = 0; i < links.size(); i++)
        {
            link = links.get(i);
            String temp = link.attr("href");

            // filtering the first 10 google links
            if (temp.contains("url") && !temp.contains("webcache"))
            {
                String[] splitTemp = temp.split("=");
                String[] splitTemp2 = splitTemp[1].split("&sa");
                System.out.println(splitTemp2[0]);
            }
        }
    }
}

代码在我的计算机上将其作为输出:

https://www.facebook.com/uniladmag/videos/1912071728815877/
https://it-it.facebook.com/DogEvolutionAsd
https://it-it.facebook.com/DylanDogSergioBonelliEditore
https://www.facebook.com/DelawareCountyDogShelter/
https://www.facebook.com/LostDogAlert/
https://it-it.facebook.com/pages/Toelettatura-Vanity-DOG/270854126382923
https://it-it.facebook.com/washdogsgm
https://www.facebook.com/thedailystar/videos/1193933410623520/
https://www.facebook.com/OakhurstDogPark/
https://www.facebook.com/bigdogdinerco/

503错误通常意味着网络服务器存在临时问题。具体做法是:

  

503:由于服务器临时过载或维护,Web服务器(运行Web站点)当前无法处理HTTP请求。这意味着这是一个暂时的条件,经过一段时间的推迟后会得到缓解。

如果此代码有效,但您的原始代码仍然没有,那么您的代码不会生成您发布的网址,您应该进一步调查。

答案 1 :(得分:0)

除了编码风格之外,我没有看到所提供代码的任何功能问题,并且它正确地提供了答案(在本地测试)。问题可能在于dateToJulian,我不知道它返回什么以及如何将结果转换为int(如果信息丢失)。

另外,考虑关键字包含危险字符并且未转义的情况。他们应该事先消毒。

另一种可能性是,如果发送太多太多内容,Google会拒绝您的查询。如果这是使用可视化浏览器完成的,那么您将获得一个&#34;我们希望确保您不是机器人。&#34;和CAPTCHA页面。这就是我建议利用Google API进行搜索的原因。有关详细信息,请参阅此SO:How can you search Google Programmatically Java API