【问题标题】:Retrieving data using WebRequest使用 WebRequest 检索数据
【发布时间】:2010-10-21 13:32:03
【问题描述】:

我从 msdn 中截取了一些代码,我很生气,因为它不能立即按照我想要的方式工作。我正在尝试使用谷歌翻译,嗯,为我翻译一些东西。问题是 t5he responseFromServer 不包含翻译后的文本,当我使用浏览器查看它时,源也不包含,尽管当查看页面本身时,chien 会自豪地显示出来。

void getTranslation()
    {
        WebRequest request = WebRequest.Create("http://translate.google.com/translate_t?hl=en#en|fr|dog");
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        int index = 0;
        while (responseFromServer.Substring(index).Contains("dog"))
        {
            index = responseFromServer.IndexOf("dog", index + 1);
            Console.WriteLine(responseFromServer.Substring(index < 50 ? 0 : index - 50, 100));
            Console.WriteLine(" ");
        }
    }

有谁知道我在这里没有理解什么?还是返回请求一样简单的翻译的网站?

【问题讨论】:

    标签: .net ajax google-translate


    【解决方案1】:

    原因是翻译请求本身是一个异步AJAX请求。如果您查看您尝试检索的页面的来源,您将找不到单词chien

    你可以看看Google AJAX Language API来实现你想要的。

    【讨论】:

    • @Patrick:您是设法满足您的要求还是最终选择了另一种方法?
    • 我将其用作学习练习,但最终没有时间跟进。我可以选择让法国的同事手动翻译我需要的内容。
    【解决方案2】:

    它不起作用,因为此应用程序使用 javascript 自动发布。

    如果您想通过屏幕截图来执行此操作,您必须使用正确的参数向表单的 URL 发出 POST 请求。

    但是,您最好只使用他们的API 而不是网络抓取。

    【讨论】:

    • 不是当您关闭 JS 并导航到 URL 时,您必须手动提交表单。我的错,我的解释应该更明确
    猜你喜欢
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多