在设置循环之前,在循环外设置变量的最佳方法是什么?(What's the best way to set a variable outside a loop before setting up the loop?)
我正在尝试使用以下代码输出日期大于或等于今天日期的事件列表:
$args = array('post_type' => 'event') // setup my custom post type $todaysdate = blah blah //setup for today's date // the wp loop query_posts($args); if ( (have_posts() && $eventdate >= $todaysdate) ) : while (have_posts()) : the_post(); $eventdate = blah blah // setup for the date of the event; echo $event; endwhile; endif;
您可以看到的问题是IF依赖于循环内的变量。
首先在循环外设置变量的最佳方法是什么?
I am trying to use the following code to output a list of events that are on a date greater than or equal to today's date:
$args = array('post_type' => 'event') // setup my custom post type $todaysdate = blah blah //setup for today's date // the wp loop query_posts($args); if ( (have_posts() && $eventdate >= $todaysdate) ) : while (have_posts()) : the_post(); $eventdate = blah blah // setup for the date of the event; echo $event; endwhile; endif;
The problem as you can see is that the IF is dependent on a variable that is within the loop.
What's the best way to set the variable outside the loop first?
原文:https://stackoverflow.com/questions/15642200
最满意答案
我认为您必须做的一件事是注入一些JavaScript来填充字段并触发登录过程。
您可以使用这样的方法(伪代码):
NSString *javaScript = @"var field = document.getElementsByTagName('#password')[0]; // Set it. // Trigger the form submission."; [webView stringByEvaluatingJavaScriptFromString:javaScript];
然后你可以通过获取内容(伪代码)来刮取页面:
NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];
One thing I think you'll have to do is inject some JavaScript to populate the fields and trigger the login process.
You can use an approach like this (pseudo code):
NSString *javaScript = @"var field = document.getElementsByTagName('#password')[0]; // Set it. // Trigger the form submission."; [webView stringByEvaluatingJavaScriptFromString:javaScript];
Then you can scrape the page by getting the content (pseudo code):
NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];
相关问答
更多-
使用XML解析器解析HTML通常无法正常工作,因为许多网站都有不正确的HTML,而Web浏览器将处理这些HTML,但严格的XML解析器(如NSXMLParser将完全失败。 对于许多脚本语言来说,有很大的更加仁慈的拼音库。 就像Python的美丽汤一样。 不幸的是,我不知道Objective-C的这些模块。 将内容加载到UIWebView可能是最简单的方法。 请注意,您不必将UIWebView放置在屏幕上。 您可以创建一个单独的UIWindow并将其添加到UIWebView中,以便完成全屏幕渲染。 有关于此 ...
-
根据这个问题 ,您可以通过“Cookie Jar”中的每个cookie进行删除,如下所示: NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *cookie in [storage cookies]) { [storage deleteCookie:cookie]; } [[NSUserDefaults standardUserDefaults] synchro ...
-
听起来您希望对应用程序中的网页之间的导航进行更精细的控制,而标准的后退/前进功能则无法实现。 我建议通过完全忽略UIWebView的内置goBack和goForward功能来简化问题,并让您的按钮委托给“StateManager”类型对象 - 根据应用程序的当前状态和用户尝试去的方向,你知道应该产生哪种状态(即:要加载哪个页面)。 要加载新状态,只需使用[_webView loadRequest:request] (或其他一个加载函数 - 见下文),其中request表示新状态。 我没有看到如何在上面的代码 ...
-
如何使用UIWebView自动抓取需要身份验证的页面?(How can UIWebView be used to automate scraping a page that requires auth?)[2022-10-03]
我认为您必须做的一件事是注入一些JavaScript来填充字段并触发登录过程。 您可以使用这样的方法(伪代码): NSString *javaScript = @"var field = document.getElementsByTagName('#password')[0]; // Set it. // Trigger the form submission."; [webView stringByEvaluatingJavaScriptFromString:javaScript]; 然后你可以通过获 ... -
如何使用RestKit和WSSE在UIWebView中进行身份验证?(How to authenticate in UIWebView using RestKit and WSSE? [closed])[2022-11-12]
如果您的下载内容是HTML,我不会看到RestKit将如何提供帮助。 UIWebView有一个loadRequest:方法,因此您应该只创建一个NSURLRequest并设置auth标头,然后将其直接传递给Web视图。 I don't see how RestKit will help if your download content is HTML. UIWebView has a loadRequest: method so you should just create an NSURLRequest ... -
由于您在webTitle声明了webTitle webViewDidFinishLoad:它是一个局部变量,您分配的任何值仅在此特定方法的范围内有效。 既然你想在webTitle范围之外使用webTitle ,那么在webViewDidFinishLoad:里面showActionSheet:你需要一个对两个方法都可见的变量。 你需要在你的webViewController声明一个实例变量来保存页面标题的值。 然后你将在webViewDidFinishLoad:设置它的值webViewDidFinishLo ...
-
当视图完成加载,成功加载和加载失败时,有两种不同的回调。 它们都是UIWebViewDelegate协议的一部分 。 实现它并将自己设置为Web视图的代表。 然后执行 - (void)webViewDidFinishLoad:(UIWebView *)webView 了解加载何时完成,以便您可以删除加载屏幕并执行 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 要知道它是否失败,以便您可以向用户显示并向 ...
-
如果auth正常工作(手动输入),也许你可以尝试为你的auth选择dormain,这样程序就知道你准备登录的确切位置。 这可以通过使用此处找到的模块request_ntlm来实现。 你使用它: from requests_ntlm import HttpNtlmAuth 以及将auth放入request.get() : auth=HttpNtlmAuth('DOMAIN HERE\\user','pass') If auth is working normally (manual enter) mayb ...
-
在UIWebView iOS中处理身份验证。这有用吗?(Handling authentication in a UIWebView iOS.How does this work?)[2021-09-22]
这是当时模拟器的一些问题。我能够在设备上运行应用程序时打印cookie。 This was some issue with the simulator at that time.I was able to print the cookies while running the app on the device. -
具有授权的UIWebView,在单击链接时使用相同的身份验证(UIWebView with authorisation, use same auth when clicking links)[2022-01-02]
如何在shouldStartLoad中正确创建新请求 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { BOOL shouldNavigate = NO; NSString *existingAuthValue = [request valueForHTT ...