网站自动登录和表格填写? [英] Automate website log-in and form filling?

查看:276
本文介绍了网站自动登录和表格填写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录到一个网站,并自动保存HTML页面(我希望能够做到这一点定期​​时间间隔)。从表面上看,这是一个典型的现代网站,如果用户直接导航到锁定的网址,一个登录形式弹出,并登录后,用户被重定向到预期的页面。

I'm trying to log in to a website and save an HTML page automatically (I want to be able to do this on a regular time interval). From the surface, this is a typical modern website where, if the user navigates directly to a "locked" URL, a log-in form pops up, and after logging in, the user is redirected to the intended page.

我给机械化了一枪( http://wwwsearch.sourceforge.net/mechanize/ )但它没有找到哪个是需要的。

I gave mechanize a shot (http://wwwsearch.sourceforge.net/mechanize/) but it wasn't finding some form elements which were needed for login (hidden elements that have some values put in by a javascript function that runs when the user clicks the "log in" button).

我打了网络浏览器控制.NET但很快就失去了兴趣一点,因为我甚至不能让它在谷歌网页上提交查询。

I played a bit with the "web browser" control in .NET but quickly lost interest because I couldn't even get it to submit a query on the Google page.

我不关心的是什么语言;我将学习它来解决这个问题。至少它在Windows中工作。

I don't care what the language is; I'll learn it to solve this problem. At a minimum it has to work in Windows.

有一个简单的例子,比如说,在查询到谷歌搜索框中输入将是一个巨大的奖金。

A simple example, say, typing in a query into the Google search box would be a great bonus.

推荐答案

在我的经验,最可靠的方法是使用JavaScript。它运作良好,在.NET。为了测试,浏览到以下地址陆续在Firefox或Internet Explorer:

In my experience, the most reliable way is to use javascript. It works well in .Net. To test, browse to the following addresses one after another in Firefox or Internet Explorer:

http://www.google.com
javascript:function f(){document.forms[0]['q'].value='stackoverflow';}f();
javascript:document.forms[0].submit()

执行对谷歌计算器的搜索。要使用WebBrowser控件做在VB的.Net,做到这一点:

That performs a search for "stackoverflow" on Google. To do it in VB .Net using the webbrowser control, do this:

WebBrowser1.Navigate("http://www.google.com")
Do While WebBrowser1.IsBusy OrElse WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    Threading.Thread.Sleep(1000)
    Application.DoEvents()
Loop
WebBrowser1.Navigate("javascript:function%20f(){document.forms[0]['q'].value='stackoverflow';}f();")
Threading.Thread.Sleep(2000) 'wait for javascript to run
WebBrowser1.Navigate("javascript:document.forms[0].submit()")
Threading.Thread.Sleep(2000) 'wait for javascript to run

注意在URL中的空间如何被转化为20%。我不能肯定,如果这是必要的,但不能伤害。该第一JavaScript的是一个函数是重要的。睡觉()的调用是等待谷歌来加载,也为JavaScript的东西。该做而如果页面无法加载这么用于自动化目的循环可能永远运行有一个计数器后,说60秒将超时。

Notice how the space in the URL is converted to %20. I'm not certain if this is necessary but it can't hurt. It is important that the first javascript be in a function. The calls to Sleep() are to wait for Google to load and also for the javascript stuff. The Do While Loop might run forever if the page fails to load so for automation purposes have a counter that will timeout after, say, 60 seconds.

当然,对于谷歌你可以直接导航到www.google.com?q=stackoverflow~~V但如果你的网站有隐藏的输入字段,等等,那么这是要走的路。仅适用于HTML网站 - 闪是一个整体的其他事项

Of course, for Google you can just navigate directly to www.google.com?q=stackoverflow but if your site has hidden input fields, etc, then this is the way to go. Only works for HTML sites - flash is a whole other matter.

这篇关于网站自动登录和表格填写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆