Java-查询google时获取HTTP错误503

Java-查询google时获取HTTP错误503,java,http,search,Java,Http,Search,我正在尝试用Java编写一个小程序,它有一个小UI,可以让你使用一些google搜索的关键字来改进你的搜索 我有两个文本字段(一个用于站点,一个用于关键字)和两个日期选择器,让用户为搜索结果选择日期范围 当我按下搜索按钮时,它将连接到以下url: "https://www.google.it/search?q=" + site + Keywords + daterange site=“site:site\u MAIN\u URL” 关键词是我正在寻找的关键词 daterange=“date

我正在尝试用Java编写一个小程序,它有一个小UI,可以让你使用一些google搜索的关键字来改进你的搜索

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

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

"https://www.google.it/search?q=" + site + Keywords + daterange 
  • site=“site:site\u MAIN\u URL”
  • 关键词是我正在寻找的关键词
  • daterange=“daterange:JULIAN\u DATE\u 1-JULIAN\u DATE\u 2”
在所有这些之后,我得到了前10个结果,但问题是

如果我没有选择日期,我可以很容易地获取链接

如果我设置了日期范围,就会出现HTTP 503错误,这是服务不可用的错误(如果我将生成的URL粘贴到web浏览器上,一切正常)

(用户代理设置为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]);
            }
        }
//我在这里生成站点
site=“site:+website\u field.getText();
//在这里,我使用在网上找到的一个类来转换日期
d1=(int)DateLabelFormatter.dateToJulian(date1);
d2=(int)DateLabelFormatter.dateToJulian(date2);
日期范围+=“+daterange:”+d1+“-”+d2;
//这里我生成关键字
关键词=关键词_字段。getText();
字符串[]关键字=关键字。拆分(“”);
对于(int i=0;i
在执行完所有这些(notsowellwrited)代码后,如果我没有选择日期,我只使用“站点”和“关键字”,我可以在控制台上看到google搜索页面上找到的前10个结果。 如果从日期选择器中选择日期范围,则会出现503错误

如果你想尝试一个有效的查询,这里有一个在facebook.com上搜索关键字“dog”,从11月1日到15日用这个“工具”生成


`

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

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]);
            }
        }
    }
}
503错误通常意味着web服务器存在临时问题。具体而言:

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


如果此代码有效,但您的原始代码仍然无效,则您的代码不会生成您发布的URL,您应该进一步调查。

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

也可以考虑关键字包含危险字符,并且它们不越界的情况。他们应该事先消毒


另一种可能性是,如果你发送太多太快,谷歌会拒绝你的查询。如果这是用视觉浏览器完成的,你会得到一个“我们想确保你不是机器人”和一个验证码页面。这就是为什么我建议在你的搜索中使用谷歌API。更多信息请参见此页:

您能提供实际通话时使用的代码吗?您使用的是简单的URL连接、Apache HTTP客户端还是其他什么?另外,您能否提供一个在浏览器中工作而不是在代码中工作的示例URL?感谢您的回复,今天尝试了,但我仍然没有得到任何503;一定是外部的问题
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/