javascript 自动填写网络表单并返回结果页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14193913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically filling out web forms and returning the resulting page
提问by Baozi
This is my first time posting here. I greatly appreciate any and all guidance on this subject.
这是我第一次在这里发帖。我非常感谢关于这个主题的任何和所有指导。
I'm trying to make a program that automatically fills in web forms and submits the data, returning the resulting page to the program so it can continue to 'browse' the page, allowing it to recursively submit even more data.
我正在尝试制作一个自动填写网络表单并提交数据的程序,将结果页面返回给程序,以便它可以继续“浏览”页面,允许它递归地提交更多数据。
The main problems I'm having are:
我遇到的主要问题是:
- The 'submit' button is coded in Javascript, so I don't know where the form data goes when making the page request.
- I want to fill in the forms using data from an Excel table, so I need to be able to access data from outside the page.
- I need to be able to navigate the resulting page to continue to submit more data.
- “提交”按钮是用 Javascript 编码的,所以我不知道在进行页面请求时表单数据的去向。
- 我想使用 Excel 表中的数据填写表单,因此我需要能够从页面外部访问数据。
- 我需要能够导航结果页面以继续提交更多数据。
More specifically, I'm trying to first login to the Practice Mate website, navigate to 'Manage Patients', hit 'Add Patients', and fill in the proper forms and submit.
I'm filling in the forms from an Excel table thousands of rows long.
Sorry I can't be more clear on this without providing a username and password.
更具体地说,我尝试首先登录到Practice Mate 网站,导航到“管理患者”,点击“添加患者”,然后填写正确的表格并提交。我正在从一个长达数千行的 Excel 表格中填写表格。
抱歉,如果不提供用户名和密码,我就无法更清楚地说明这一点。
What I've been trying to do is use Javascript to make page requests from a page that retrieves information from the Excel document using PHP. I still can't seem to get anything to work with this method though.
我一直在尝试做的是使用 Javascript 从使用 PHP 从 Excel 文档中检索信息的页面发出页面请求。尽管如此,我似乎仍然无法使用这种方法。
I apologize for being a relative novice at this. Thanks in advance.
我很抱歉在这方面相对新手。提前致谢。
回答by Pastor Bones
You can use PHP cURLto browse & submit forms to websites, but it does depend on how the website is setup. Most have security checks in place to prevent bots and can be tricky to get everything to work right.
您可以使用PHP cURL浏览和提交表单到网站,但这取决于网站的设置方式。大多数都有安全检查来防止机器人,并且很难让一切正常工作。
I spent a little bit of time and came up with this login script. Without a valid username and password I can't verify that it is successful, but should do what you need. This short example first browses to the page to set any cookies and scrape a __VIEWSTATE value needed to submit the form. It then submits the form using the username/password you provide.
我花了一点时间,想出了这个登录脚本。没有有效的用户名和密码,我无法验证它是否成功,但应该做你需要的。这个简短的示例首先浏览页面以设置任何 cookie 并抓取提交表单所需的 __VIEWSTATE 值。然后使用您提供的用户名/密码提交表单。
<?php
// Login information
$username = 'test';
$password = 'mypass';
$utcoffset = '-6';
$cookiefile = '/writable/directory/for/cookies.txt';
$client = new Client($cookiefile);
// Retrieve page first to store cookies
$page = $client -> get("https://pm.officeally.com/pm/login.aspx");
// scrape __VIEWSTATE value
$start = strpos($page, '__VIEWSTATE" value="') + 20;
$end = strpos($page, '"', $start);
$viewstate = substr($page, $start, $end - $start);
// Do our actual login
$form_data = array(
'__LASTFOCUS' => '',
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => $viewstate,
'hdnUtcOffset' => $utcoffset,
'Login1$UserName' => $username,
'Login1$Password' => $password,
'Login1$LoginButton' => 'Log In'
);
$page = $client -> get("https://pm.officeally.com/pm/login.aspx", $form_data);
// cURL wrapper class
class Login {
private $_cookiefile;
public function __construct($cookiefile) {
if (!is_writable($cookiefile)) {
throw new Exception('Cannot write cookiefile: ' . $cookiefile);
}
$this -> _cookiefile = $cookiefile;
}
public function get($url, $referer = 'http://www.google.com', $data = false) {
// Setup cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this -> _cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this -> _cookiefile);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
// Is there data to post
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
return curl_exec($ch);
}
}
回答by whitehatceo
Well, I think the cURL will do the trick, the curl_init()
handler is explicable enough. Still at the inception of the doc peruse, howbeit, good results are anticipated. Well, not too sure about the PHP flexibility of structures as that will mean a lot with cURL. Hope to find good luck down the line.
好吧,我认为 cURL 可以解决问题,curl_init()
处理程序已经足够解释了。仍然在文档阅读的开始阶段,但是,预期会有好的结果。好吧,不太确定结构的 PHP 灵活性,因为这对 cURL 意义重大。希望能找到好运。
回答by newrev426
Also check out the question I asked that is similar. That is how to do it in C# atleast for a website I encountered.
另请查看我提出的类似问题。至少对于我遇到的网站,这就是如何在 C# 中做到这一点。