【问题标题】:How to get data from website如何从网站获取数据
【发布时间】:2014-12-29 17:12:05
【问题描述】:

我需要一个包含标签文本的变量。但是标签在aspx的网站中。 我尝试使用 NSURLSession,但我不明白哪种方法是正确的。

【问题讨论】:

    标签: ios swift nsurlsession foundation


    【解决方案1】:

    这个方法很简单

    1) 从url获取html内容:可以使用 - NSURLSession NSURLSession *会话 = [NSURLSession sessionWithConfiguration:sessionConfig 代表:无 委托队列:无];

    [[session dataTaskWithURL:[NSURL URLWithString:urlString]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {
    

    - NSURLConnection:

    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self];
    
    • dataWithContentsOfURL:

      NSData * htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];

    2) 使用 TFHpple 从该内容中解析变量内容

    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
    NSArray *elements  = [xpathParser search:@"//variable"]; // get the variable
    

    希望对你有帮助

    【讨论】:

    • 正确。你可以谷歌看看 swift 版本
    猜你喜欢
    • 2021-08-29
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多