iOS 中使用JS提取网页数据,自动提交表单等
2016-06-07 10:03
701 查看
1.首先用一个webview加载网页
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,
0, SCREEN_WIDTH, SCREEN_HEIGHT)];
webView.delegate = self;
[self.view addSubview:webView];
NSURL *url =[[NSURL alloc] initWithString:@"http://www.reg007.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
2.自动提交表单
NSString *ret = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='18666666666';"];
NSLog(@"%@", ret);
ret = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];
NSLog(@"%@", ret);
3.分析网页数据
NSString *count = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('li').length;"];
NSLog(@"%d %@",
0, count);
if (!count) {
return;
}
for (int i=0; i<[count intValue]; i++) {
NSString *classType = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].getAttribute(\"class\");", i]];
if ([classType isEqualToString:@"media"]) {
NSLog(@"%d %@\n", i+1, count);
NSString *name = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].childNodes[1].childNodes[0].childNodes[0].textContent;", i]];
NSString *href = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].childNodes[1].childNodes[0].childNodes[0].href;", i]];
NSString *desc = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].childNodes[1].childNodes[3].textContent;", i]];
NSLog(@"name: %@, href:%@, desc:%@\n\n", name, href, desc);
}
}
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,
0, SCREEN_WIDTH, SCREEN_HEIGHT)];
webView.delegate = self;
[self.view addSubview:webView];
NSURL *url =[[NSURL alloc] initWithString:@"http://www.reg007.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
2.自动提交表单
NSString *ret = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='18666666666';"];
NSLog(@"%@", ret);
ret = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];
NSLog(@"%@", ret);
3.分析网页数据
NSString *count = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('li').length;"];
NSLog(@"%d %@",
0, count);
if (!count) {
return;
}
for (int i=0; i<[count intValue]; i++) {
NSString *classType = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].getAttribute(\"class\");", i]];
if ([classType isEqualToString:@"media"]) {
NSLog(@"%d %@\n", i+1, count);
NSString *name = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].childNodes[1].childNodes[0].childNodes[0].textContent;", i]];
NSString *href = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].childNodes[1].childNodes[0].childNodes[0].href;", i]];
NSString *desc = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementsByTagName('li')[%d].childNodes[1].childNodes[3].textContent;", i]];
NSLog(@"name: %@, href:%@, desc:%@\n\n", name, href, desc);
}
}
相关文章推荐
- iOS通过经纬度计算两个地点之间的距离
- iOS开发拓展篇—静态库
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
- IOS AVAudioRecorder 录制音频
- iOS 宏(define)与const的正确用法
- iOS 生成二维码 官方版
- iOS中读取照片库及保存图片或视频到照片库的要点解析
- iOS 9开发小技巧
- 快速掌握iOS之Framework制作流程
- 开源项目之Coding-iOS
- iOS 开发技术牛人博客
- 在iOS上实现一个简单的日历控件
- iOS 原生导航栏 修改导航栏文字颜色 大小
- 细说 iOS 消息推送
- 国际化浅析。
- iOS中的动画
- IOS博客项目搭建-20-项目重构-封装请求参数和返回的结果
- ios 自适应布局
- iOS多线程编程(一)——RunLoop基本概念
- iOS开发笔记(二)