1

嗨,我编写了以下脚本来登录网站。我现在要做的是在我们登录后看到的页面上执行搜索。目前我的脚本在您登录后返回页面。它有一个带有搜索字段和按钮的表单。我还注意到它使用 __VIEWSTATE 和 __EVENTVALIDATION 作为其表单的一部分。我知道这两个字段的值并不总是相同的。所以我想知道当我执行搜索时如何从表单中检索这些值,以便我可以使用它们在我的脚本中发布表单。这是我用来登录的代码:

    <?php

$post_data['ctl00$MainContent$EmailText'] = 'xxxx@xxxx.com';
$post_data['ctl00$MainContent$PasswordText'] = 'xxxx';
$post_data['ctl00$MainContent$LogInButton'] = 'Log On';

foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

$post_string = implode ('&', $post_items);

$curl_connection =
  curl_init('https://www.XXXX.co.uk/Login.aspx');
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_connection, CURLOPT_COOKIEJAR, $ckfile); 

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($curl_connection);

curl_close($curl_connection);

?>
4

1 回答 1

1

您最好的选择可能是使用 PHP 的 DOMDocument 类来遍历返回的 HTML 并获取您要查找的内容。您可以将结果字符串加载到 DOMDocument 中,然后使用getElementsByTagNameorgetElementById来获取节点。如果输入元素具有 id 值,则首选后者。

实现看起来像:

// $result is string returned by cURL from your code
$dom = new DOMDocument();
$dom->loadHTML($result);
$node = $dom->getElementById('your_element_id');
$node_value = $node->getAttribute('value');
于 2012-09-17T15:11:25.173 回答