【问题标题】:iOS html/xml parsing google shopping results with TFHppleiOS html/xml 使用 TFHpple 解析谷歌购物结果
【发布时间】:2013-07-21 10:33:58
【问题描述】:

有没有什么方法可以使用 TFHpple 解析谷歌购物结果而不使用谷歌 API(已弃用),但使用 url 很简单,例如:https://www.google.com/search?hl=en&tbm=shop&q=AudiR8

我尝试了很多类型的标签:

...
myCar = @"Audi R8";
myURL = [NSString stringWithFormat:@"https://www.google.com/search?hl=en&tbm=shop&q=%@",myCar];
NSData *htmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];
TFHpple *xpath = [[TFHpple alloc] initWithHTMLData:htmlData];
//use xpath to search element
NSArray *elements = [NSArray new];
elements = [xpath searchWithXPathQuery:@"//html//body"]; // <-- tags
...

但无事可做,总是相同的输出控制台消息:无法解析。

【问题讨论】:

    标签: ios parsing tfhpple


    【解决方案1】:

    您可以尝试更改以下行

    NSData *htmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];
    

    NSData *Data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];
    

    还有下面一行

    TFHpple *xpath = [[TFHpple alloc] initWithHTMLData:htmlData];
    

    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
    

    如果这有帮助,请告诉我,否则您可能需要在代码中更改另一行。

    编码愉快!

    【讨论】:

    • 更改 NSData 类型变量的名称或更改解析器变量的名称应该改变什么?如果您想知道结果显然为零,​​则没有任何变化。
    【解决方案2】:

    我发现了各种问题,最后我都解决了。 首先需要对 URL 添加编码:

    myURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    

    然后,在原始(和实际)TFHPPLE 代码(确切地说是 XPathQuery.m)中,解析阶段会崩溃,因为任何时候 nodeContent 和 Raw 都是 NIL。 所以,为了解决这个崩溃,我改变了

    [resultForNode setObject:currentNodeContent forKey:@"nodeContent"];
    

    with (注意两行 [resultForNode...:

    if (currentNodeContent != nil)
       [resultForNode setObject:currentNodeContent forKey:@"nodeContent"];
    

    和:

    [resultForNode setObject:rawContent forKey:@"raw"];
    

    与:

    if (rawContent != nil)
          [resultForNode setObject:rawContent forKey:@"raw"];
    

    我想记住这一点,因为 google 使用更难的 html 代码,我决定使用这些 xpathqueries:

    ...
            NSArray *elementsImages = [NSArray new];
            NSArray *elementsPrices = [NSArray new];
            elementsImages = [xpath searchWithXPathQuery:@"//html//*[@class=\"psliimg\"]"];
            elementsPrices = [xpath searchWithXPathQuery:@"//html//*[@class=\"psliprice\"]"];
    ...
    

    另一个不便之处是当您决定使用 for 或 while 循环来检索各种 html 页面时,实际上如果您使用:

    NSData *htmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];
    

    initWithContenctsOfURL 在循环中多次无法正确获取页面(并且调试控制台编写了著名的 UNABLE TO PARSE )所以我决定将其更改为:

    // Send a synchronous request
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]];
    NSURLResponse * response = nil;
    NSError * error = nil;
    NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
                                              returningResponse:&response
                                                          error:&error];
    
    if (error == nil)
    {
        // Parse data here
    }
    

    如果您不想等待这个循环,因为它是由同步 NSURLRequests 产生的,请尝试调用父方法(并且您的视图控制器不会冻结等待解析器):

    _dispatch_queue_t *queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                        dispatch_async( _queue, // now i call my google shopping parser cycle
                        ^{
                            [self GShoppingParser];
    });
    

    【讨论】:

      猜你喜欢
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 2012-07-29
      • 1970-01-01
      • 2020-05-20
      • 2015-11-26
      • 1970-01-01
      相关资源
      最近更新 更多