Streamwriter.WriteLine()不是写一切。(Streamwriter.WriteLine() is not writing everything. Strange output)

我正在编写一个程序,以便链接到我的大学教师生物页面。 我正在使用HTMLAgilityPack。 这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.IO;

namespace Get_Professor_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("Links.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            string url, previousurl = "";
            char c = '@';
            StreamWriter writer = new StreamWriter(fs);
            HtmlWeb web = new HtmlWeb();
            for (int i = 0; i < 26; i++)
            {
                HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
                {
                    c++;
                    url = link.Attributes["href"].Value.ToString();
                    //if (url == previousurl)
                    //    continue;
                    try
                    {
                        if (url.Substring(0, 25).Equals(@"/facultybios/profile.php?", StringComparison.Ordinal))
                        {
                            writer.WriteLine(@"https://www2.aus.edu" + url);
                            writer.Flush();
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    previousurl = url;
                }
            }
            writer.Close();
        }
    }
}

这是我的输出:

https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=jabdalla
https://www2.aus.edu/facultybios/profile.php?faculty=jsater
https://www2.aus.edu/facultybios/profile.php?faculty=jgriffin
https://www2.aus.edu/facultybios/profile.php?faculty=jfedtke
https://www2.aus.edu/facultybios/profile.php?faculty=jyounas
https://www2.aus.edu/facultybios/profile.php?faculty=jsqualli
https://www2.aus.edu/facultybios/profile.php?faculty=jboisvert
https://www2.aus.edu/facultybios/profile.php?faculty=jvinke
https://www2.aus.edu/facultybios/profile.php?faculty=jbaker
https://www2.aus.edu/facultybios/profile.php?faculty=jhassan
https://www2.aus.edu/facultybios/profile.php?faculty=jpalmer
https://www2.aus.edu/facultybios/profile.php?faculty=jkolo
https://www2.aus.edu/facultybios/profile.php?faculty=jmarch
https://www2.aus.edu/facultybios/profile.php?faculty=jinhyuk
https://www2.aus.edu/facultybios/profile.php?faculty=giesen
https://www2.aus.edu/facultybios/profile.php?faculty=jvangorp
https://www2.aus.edu/facultybios/profile.php?faculty=jswanstrom
https://www2.aus.edu/facultybios/profile.php?faculty=jking
https://www2.aus.edu/facultybios/profile.php?faculty=jmontague
https://www2.aus.edu/facultybios/profile.php?faculty=jallee
https://www2.aus.edu/facultybios/profile.php?faculty=jkatsos
https://www2.aus.edu/facultybios/profile.php?faculty=jbley
https://www2.aus.edu/facultybios/profile.php?faculty=jwallis
https://www2.aus.edu/facultybios/profile.php?faculty=jgibbs
https://www2.aus.edu/facultybios/profile.php?faculty=jroldan
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https

出于某些奇怪的原因,只打印J页面中的链接。 有些链接是空的。 最后一行只有https(这就是为什么我认为问题在于编写器而不是我的代码的逻辑)。 我一直试图解决这个问题一段时间没有运气。

这些是我正在抓取的页面: https//www2.aus.edu/facultybios/

任何帮助,将不胜感激。


I'm writing a program to scrape links to my universities faculty bios pages. I'm using HTMLAgilityPack. This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.IO;

namespace Get_Professor_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("Links.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            string url, previousurl = "";
            char c = '@';
            StreamWriter writer = new StreamWriter(fs);
            HtmlWeb web = new HtmlWeb();
            for (int i = 0; i < 26; i++)
            {
                HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
                {
                    c++;
                    url = link.Attributes["href"].Value.ToString();
                    //if (url == previousurl)
                    //    continue;
                    try
                    {
                        if (url.Substring(0, 25).Equals(@"/facultybios/profile.php?", StringComparison.Ordinal))
                        {
                            writer.WriteLine(@"https://www2.aus.edu" + url);
                            writer.Flush();
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    previousurl = url;
                }
            }
            writer.Close();
        }
    }
}

This is my output:

https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=jabdalla
https://www2.aus.edu/facultybios/profile.php?faculty=jsater
https://www2.aus.edu/facultybios/profile.php?faculty=jgriffin
https://www2.aus.edu/facultybios/profile.php?faculty=jfedtke
https://www2.aus.edu/facultybios/profile.php?faculty=jyounas
https://www2.aus.edu/facultybios/profile.php?faculty=jsqualli
https://www2.aus.edu/facultybios/profile.php?faculty=jboisvert
https://www2.aus.edu/facultybios/profile.php?faculty=jvinke
https://www2.aus.edu/facultybios/profile.php?faculty=jbaker
https://www2.aus.edu/facultybios/profile.php?faculty=jhassan
https://www2.aus.edu/facultybios/profile.php?faculty=jpalmer
https://www2.aus.edu/facultybios/profile.php?faculty=jkolo
https://www2.aus.edu/facultybios/profile.php?faculty=jmarch
https://www2.aus.edu/facultybios/profile.php?faculty=jinhyuk
https://www2.aus.edu/facultybios/profile.php?faculty=giesen
https://www2.aus.edu/facultybios/profile.php?faculty=jvangorp
https://www2.aus.edu/facultybios/profile.php?faculty=jswanstrom
https://www2.aus.edu/facultybios/profile.php?faculty=jking
https://www2.aus.edu/facultybios/profile.php?faculty=jmontague
https://www2.aus.edu/facultybios/profile.php?faculty=jallee
https://www2.aus.edu/facultybios/profile.php?faculty=jkatsos
https://www2.aus.edu/facultybios/profile.php?faculty=jbley
https://www2.aus.edu/facultybios/profile.php?faculty=jwallis
https://www2.aus.edu/facultybios/profile.php?faculty=jgibbs
https://www2.aus.edu/facultybios/profile.php?faculty=jroldan
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https://www2.aus.edu/facultybios/profile.php?faculty=
https

For some strange reason only the links from the J page are printed. Some links are empty. The last line has only https (which is why I think the problem is with the writer and not the logic of my code). I've been trying to resolve this for a while with no luck.

These are the pages I'm scraping from: https://www2.aus.edu/facultybios/

Any help would be appreciated.


原文:https://stackoverflow.com/questions/26933855
2021-12-03 07:12

满意答案

我完全同意Jon的观察结果:你根本不需要捕获异常(相反,只需在调用Substring()之前检查长度!),但是肯定你应该只捕获你期望获得的异常。 你应该使用using来处理同时处理FileStream对象和StreamWriter对象(从技术上讲,后者为你配置了前者,但恕我直言,这是明白的好事)。

至于实际问题,在我看来,有一个明显的错误,一个可能的错误:

  • 显而易见的错误是你在错误的范围内递增c (你用来选择要刮的页面的变量)。 也就是说,您为每个处理的URL增加一次值。 据推测,你实际上想要在循环之前增加该变量,而不是在循环内部。
  • 即代替这个:

    HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
        c++;
    

    你可能想写这个:

    HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
    c++;
    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
    

    或者甚至可能是这样:

    HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + (c++));
    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
    
  • 可能的错误是你将c初始化为字符@ 。 我没有看到该页面上的任何内容表明这将是一个有效的角色使用; 它看起来只有当sort参数设置为从AZ的字母时才会显示链接(不区分大小写)。
  • 考虑到所有这些,恕我直言,编写此代码的更好方法是这样的:

    using (FileStream fs = new FileStream("Links.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)_
    using (StreamWriter writer = new StreamWriter(fs))
    {
        string url;
        HtmlWeb web = new HtmlWeb();
        for (int i = 0; i < 26; i++)
        {
            char c = (char)('A' + i);
            HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
            {
                url = link.Attributes["href"].Value.ToString();
                if (url.Length > 25 &&
                    url.Substring(0, 25).Equals(@"/facultybios/profile.php?", StringComparison.Ordinal))
                {
                    writer.WriteLine(@"https://www2.aus.edu" + url);
                    writer.Flush();
                }
            }
        }
    }
    

    I agree 100% with Jon's observations: you should not need to catch exceptions at all (instead, just check the length before calling Substring()!), but for sure you should only catch an exception you expect to get. And you should use using to handle disposing both the FileStream object and the StreamWriter object (technically, the latter disposes the former for you, but IMHO it's just good to be explicit).

    As for the actual problem, it seems to me that there's one obvious bug, and one possible bug:

  • The obvious bug is that you are incrementing c (the variable you're using to select which page to scrape) in the wrong scope. That is, you increment its value once for every URL you process. Presumably, you actually want to increment that variable just before the loop, instead of inside of it.
  • I.e. instead of this:

    HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
        c++;
    

    you probably meant to write this:

    HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
    c++;
    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
    

    Or maybe even this:

    HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + (c++));
    foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
    {
    
  • The possible bug is that you initialize c to the character @. I didn't see anything on that page to suggest that would be a valid character to use; it looks like it will display links only when the sort parameter is set to a letter from A through Z (not case-sensitive).
  • With all of that in mind, IMHO a better way to write this code would be something like this:

    using (FileStream fs = new FileStream("Links.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)_
    using (StreamWriter writer = new StreamWriter(fs))
    {
        string url;
        HtmlWeb web = new HtmlWeb();
        for (int i = 0; i < 26; i++)
        {
            char c = (char)('A' + i);
            HtmlDocument doc = web.Load(@"https://www2.aus.edu/facultybios/index.php?sort=" + c);
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
            {
                url = link.Attributes["href"].Value.ToString();
                if (url.Length > 25 &&
                    url.Substring(0, 25).Equals(@"/facultybios/profile.php?", StringComparison.Ordinal))
                {
                    writer.WriteLine(@"https://www2.aus.edu" + url);
                    writer.Flush();
                }
            }
        }
    }
    

    相关问答

    更多

    使用StreamWriter和Console输出保存x行数(Save x amount of lines with StreamWriter and Console output)

    这是我在上面的评论中提出的建议的代码。 // Keeps most recent 100–200 lines. List<string> cache = new List<string>(); while (true) { // Create new writer, overwriting old file (if it already exists). using (var streamWriter = new StreamWriter(/* ... */)) { ...

    Streamwriter写入不同的Console.WriteLine()(Streamwriter writes different then Console.WriteLine())

    看起来作者只写了每一行的结尾。 这是因为您每次都重新打开文件,覆盖以前的内容。 您需要在调用ShowOutput之间保持文件打开,或者在Append模式下打开它。 文件中的混合内容是因为它只覆盖文件的开头(不截断它),因此如果写入比前一个更短的日志条目,则前一个文本的结尾仍然存在。 It looks like the writer only writes the end of every Line. This is because you re-open the file every time, ...

    Streamwriter.Writeline和File.WriteAllText不按顺序写文本(Streamwriter.Writeline and File.WriteAllText writing text out of order)

    XML属性的顺序并不重要,所以我不担心它。 但是,如果它真的在烦你,我建议移动你的使用声明。 using (StreamWriter sw = File.AppendText(@"C:\Users\status.xml")) { sw.WriteLine(contents); } 虽然这可能是优化的,所以它的工作方式与编写得更好一样,但是当前有一个新的StreamWriter方式正在被分配,然后在每次迭代时被处理掉。 你的using语句应该包含while循环,而不是相反。 这可能解决问题...

    Streamwriter.WriteLine()不是写一切。(Streamwriter.WriteLine() is not writing everything. Strange output)

    我完全同意Jon的观察结果:你根本不需要捕获异常(相反,只需在调用Substring()之前检查长度!),但是肯定你应该只捕获你期望获得的异常。 你应该使用using来处理同时处理FileStream对象和StreamWriter对象(从技术上讲,后者为你配置了前者,但恕我直言,这是明白的好事)。 至于实际问题,在我看来,有一个明显的错误,一个可能的错误: 显而易见的错误是你在错误的范围内递增c (你用来选择要刮的页面的变量)。 也就是说,您为每个处理的URL增加一次值。 据推测,你实际上想要在循...

    输出文件不会从StreamWriter中出现(Output file does not appear from StreamWriter)

    非常感谢@Ansgar Wiechers。 我可以将文件放入正确的目录的唯一方法是从根生成完整路径。 由于我想把它放到当前目录中, (Get-Location).Path提供了我追加文件名的目录路径。 没有指定完整的路径, System.IO.StreamWriter想把文件放到我的主目录中。 PS C:\Users\lit\t\booo> Get-Content .\ttt.ps1 [cmdletbinding()] Param() $FullPath = Join-Path (Get-Loc...

    将Console.WriteLine()输出重定向到字符串(Redirect Console.WriteLine() output to string)

    使用StringWriter : var sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); string result = sw.ToString(); Use a StringWriter: var sw = new StringWriter(); Console.SetOut(sw); Console.SetError(sw); string result = sw.ToString();

    当BufferBlock为空时,Async StreamWriter循环停止写入(Async StreamWriter Loop stops writing when BufferBlock is empty)

    这与Dataflow块无关。 发生这种情况的原因是因为StreamWriter和FileStream使用缓冲更有效。 因此,您应该做的是在循环结束时添加await writer.FlushAsync() ,这会清空缓冲区并将数据写入磁盘。 This has nothing to do with the Dataflow block. The reason this happens is because StreamWriter and FileStream use buffering to be ...

    奇怪的解密输出(Strange Decryption Output)

    试试这个: void encrypt(const std::string& r){ std::string new_r; for(int i = 0; i < r.length(); i++) { new_r += ~r[i]; } wtofe("/home/programming/Desktop/latin.txt", new_r); // Writes it to a file decrypt(new_r); } void decrypt(const s...

    在非ASP.NET应用程序中生成HTML的最简单方法是什么?(what's the easiest way to generate HTML in a non-ASP.NET application?)

    我做了很多自动HTML页面生成。 我喜欢创建一个带有自定义标记的HTML页面模板,用于插入动态控件,数据或文字。 然后,我将模板文件读入一个字符串,并将生成的HTML替换为自定义标记,就像上面所做的那样,并将HTML文件写回字符串中。 这为我节省了为设计模板,css和支持JS创建所有繁琐的支持HTML的时间。 模板文件示例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html xmlns="http://www....

    debug writeline有奇怪的输出(debug writeline has strange output)

    Debug.WriteLine(string,string) method call is different from Debug.WriteLine(string,object[]) 这可能是为什么第一个WriteLine语句正在工作,而其余的不是,因为第二个参数可能是一个字符串。 请在这里查看文档 Debug.WriteLine(string,string) method call is different from Debug.WriteLine(string,objec...

    相关文章

    更多

    javax.imageio.IIOException: Can't create output stream!的解决方案

    ImageIO.write(image, "jpeg", response.getOutputStre ...

    Hadoop 报错be replicated to 0 nodes, instead of 1

    1,现象:flume再往Hadoop HDFS写文件时flume.log报错 be replicate ...

    用PHP写Hadoop的MapReduce程序

    Hadoop流 虽然Hadoop是用java写的,但是Hadoop提供了Hadoop流 ...

    Jackson ObjectMapper实现JSON实际的读/写

    以下是org.codehaus.jackson.map.ObjectMapper类的声明

    MapReduce TotalOrderPartitioner 全局排序

    我们知道Mapreduce框架在feed数据给reducer之前会对map output key排序, ...

    Hadoop实战原理总结

    用户提交给Hadoop client的command,指定了输入路径,输出路径,如下所示: cmd=& ...

    Storm【实践系列-如何写一个爬虫】 - ParserBolt

    阅读背景: 如果您对爬虫,或则web前端不够了解,请自行google。 代码前提:您需要参阅本ID 所 ...

    Storm【实践系列-如何写一个爬虫4】 - ParserBolt

    代码精要: 1 : 首先,在新版本Storm0.92的功能中加入了Storm Metric的度量 2 ...

    又开始写blog啦

    我经常会有一些想法,尤其在等车或者坐公交车的时候。 常常想过了就忘了,干嘛不找个地方记下来呢? 发微博 ...

    开始写博客

    一直以来都想开博客,这么多年的工程工作经验都没有积累,实在是可惜啊。每每遇到了问题,依稀记得曾经碰到过 ...

    最新问答

    更多

    将十六进制的字符串转换为字符串(Convert hex-encoded String to String)

    您希望将十六进制编码的数据用作AES密钥,但数据不是有效的UTF-8序列。 您可以将其解释为ISO Latin编码中的字符串,但AES(key: String, ...)初始化程序将该字符串转换回其UTF-8表示形式,也就是说,您将从开始时获得不同的关键数据。 所以你不应该把它转换成字符串。 使用 extension Data { init?(fromHexEncodedString string: String) } 方法从Swift中进行十六进制/二进制字符串转换,将十六进制编码的

    将friendly_id添加到用户模型后,登录后的友好转发不起作用(Friendly forwarding after login doesn't work after adding friendly_id to the user model)

    在应用程序控制器中,将redirect_back_or(path)方法更改为此方法。 def redirect_back_or(path) redirect_to session[:forwarding_url] || path session.delete(:forwarding_url) end 你有redirect_to request.referer之前负责将你重定向到最后一个url,即login_url。 我不知道为什么你要定义两个不同的方法

    Qt + VS2010:由于 .dll从您的计算机丢失,程序无法启动(Qt + VS2010: The program can't start because .dll is missing from your computer)

    您可能需要将有问题的DLL复制到您的可执行文件所在的文件夹中,或者确保DLL位于系统PATH中的文件夹中。 You likely need to copy the DLLs in question to the folder your executable is in, or ensure that the DLLs are located in a folder in the system's PATH.

    AutoFixture使用内部setter创建属性(AutoFixture create property with internal setter)

    理想情况下 ,测试不应该与类的internal成员交互,因为它们明确地从其公共API中排除 。 相反,这些成员将通过公共API启动的代码路径间接测试。 但是,如果在您的特定情况下这不可行,则可能的解决方法是从测试中明确地为内部属性赋值 。 您可以通过以下两种方式之一来实现: 通过使用InternalsVisibleTo属性将程序集中的所有内部成员公开给测试项目。 通过在特定接口中表示类的可修改状态并明确地实现它。 在您的示例中,选项1将是: // [assembly:InternalsVisib

    使用Trigger.IO/PhoneGap在UIWebView中使用focus()事件自动显示键盘(Show keyboard automatically with focus() event in UIWebView using Trigger.IO/PhoneGap)

    访问UIWebView不是我们在当前版本的插件中正确公开的东西(但我们很快就会支持)。 现在,如果您想尝试一下,可以添加 extern UIWebView *webView; 在插件文件的顶部,这将使您的API方法中的变量webView可用。 这将很快停止工作,所以我建议你现在只用它来测试一下。 2012年10月更新 : 该应用程序的Web视图现在通过ForgeApp : http : ForgeApp 例如: [ForgeApp sharedApp].webView Access to t

    ASP.NET MVC控件(ASP.NET MVC Control)

    您可以使用RadComboBox以及此处找到的此telerik社区项目提供的一些小调整。 应该在MVC中都能正常工作。 You can use the RadComboBox along with a little tweaking as provided for by this telerik community project, found here. Should all work fine in MVC.

    在后期保存上下文 - 将指针保存到上下文?(Saving Context At a Later Stage - Saving Pointer To Context ? Core Data)

    是的,您可以在循环后保存上下文。 它比每次迭代中的保存要好得多。 如果你看一下MagicalRecord src,你会看到MR_contextForCurrentThread总是为相同的线程返回相同的上下文,如果没有上下文的线程,MagicalRecord会创建它。 此外,您不需要传递上下文[_entityClass createInContext:context] ,只需要[_entityClass MR_createEntity] - 它将在当前线程的上下文中创建 Yes, you can

    tinyMce函数用于确定弹出窗口是否已打开(tinyMce function to determine if popup is already open)

    这是我要去的解决方案: 我似乎已经确认: - windowManager.open()不会像window.open()那样返回对窗口的引用 - windowManager没有内置方法来限制可以打开的实例数。 但它确实有一种方法可以将onClose函数添加到插件窗口:ed.windowManager.onClose.add(function(){alert('Closing!');}); 所以我将在onClick代码中使用一个变量来跟踪弹出窗口是否已被打开和关闭。 onClose函数将该变量标记为

    使用nil调用Document.find在mongodb中无效(Calling Document.find with nil is invalid in mongodb)

    OrderController #new调用OrderController#current_cart,它运行Cart.find(session [:cart_id])。 在会话开始时没有:cart_id,即session [:cart_id]为nil,你得到上面的Mongoid :: Errors :: InvalidFind异常。 请注意,当您正在抢救ActiveRecord :: RecordNotFound时,您的救援子句不会挽救该异常。 您正在使用Mongoid,而不是ActiveRec

    在函数中返回postgresql查询结果(Return postgresql query result in a function)

    假设您正在使用nodejs和pg模块。 正如您所说,由于查询功能是异步的,您无法直接将结果返回给调用者。 传统上,在nodejs中,调用者传递一个回调函数来处理结果或错误(如果有的话)。 在Kotlin中,这看起来像: client.query(MY_QUERY_TEMPLATE, params) { err, result -> if (err != null) { // do something with the error }