【问题标题】:Java - Getting HTTP error 503 when querying googleJava - 查询谷歌时出现 HTTP 错误 503
【发布时间】:2015-11-16 15:16:27
【问题描述】:

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

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

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

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

在这一切之后,我获取了前 10 个结果,但这是问题所在......

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

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

(用户代理设置为 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)代码后,如果我不选择日期,并且我只使用“站点”和“关键字”,我可以在控制台上看到在谷歌搜索页面上找到的前 10 个结果。 如果我从日期选择器中选择一个日期范围,我会收到 503 错误。

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

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

`

【问题讨论】:

  • 您能否提供用于进行实际调用的代码?您使用的是简单的 URLConnection、Apache HTTP 客户端还是其他任何东西?
  • 另外,您能否提供一个生成的示例 URL,该 URL 可以在浏览器中运行,而不是代码?

标签: java http search


【解决方案1】:

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

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 服务器(运行网站)当前无法处理 HTTP 请求。言下之意,这是一种暂时的情况,经过一段时间后会得到缓解。

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

【讨论】:

  • 感谢回复,今天试了一下,还是没有503;这一定是一些外部问题
【解决方案2】:

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

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

另一种可能性是,如果您发送太多太快的查询,Google 会拒绝您的查询。如果这是使用可视浏览器完成的,你会得到“我们要确保你不是机器人”。和验证码页面。这就是为什么我建议您使用 Google API 进行搜索。有关更多信息,请参阅此 SO:How can you search Google Programmatically Java API

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 2018-06-06
    • 2018-03-15
    • 1970-01-01
    • 2011-01-04
    • 2021-09-26
    • 1970-01-01
    • 2018-06-03
    相关资源
    最近更新 更多