在 PHP 中使用 POST 变量抓取 ASP.Net 网站

发布于 2024-09-19 07:17:35 字数 2294 浏览 8 评论 0原文

在过去的几天里,我一直在尝试抓取一个网站,但到目前为止还没有成功。

情况如下: 我试图抓取的网站需要之前提交的表单中的数据。我已经识别了 Web 应用程序所需的变量,并调查了原始 Web 应用程序发送的 HTTP 标头。

由于我对 ASP.net 的了解几乎为零,所以我想问一下我是否在这里遗漏了一些东西。

我尝试了不同的方法(CURL、获取内容和 Snoopy 类),这是我的 curl 方法的代码:

<?php
$url = 'http://www.urltowebsite.com/Default.aspx';
$fields = array('__VIEWSTATE' => 'averylongvar',
                '__EVENTVALIDATION' => 'anotherverylongvar',
                'A few' => 'other variables');

$fields_string = http_build_query($fields);

$curl = curl_init($url);

curl_setopt_array
(
    $curl,
    array
    (
        CURLOPT_RETURNTRANSFER  =>    true,
        CURLOPT_SSL_VERIFYPEER  =>    0,  //    Not supported in PHP
        CURLOPT_SSL_VERIFYHOST  =>    0,  //        at this time.
        CURLOPT_HTTPHEADER      =>
            array
            (
                'Content-type: application/x-www-form-urlencoded; charset=utf-8',
                'Set-Cookie: ASP.NET_SessionId='.uniqid().'; path: /; HttpOnly'
            ),
        CURLOPT_POST            =>    true,
        CURLOPT_POSTFIELDS      =>    $fields_string,
        CURLOPT_FOLLOWLOCATION => 1
    )
);

$response = curl_exec($curl);
curl_close($curl);

echo $response;
?>

请求了以下标头:

请求标头

  • Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5
  • Content-类型:application/x-www-form-urlencoded
  • 用户代理:Mozilla/5.0 (Macintosh; U; 英特尔 Mac OS X 10_6_4; en-us) AppleWebKit/533.18.1(KHTML,如 Gecko) Version/5.0.2 Safari/533.18.5

表单数据

  • 很多表单字段

响应标头

  • Cache-Control:private
  • Content-Length:30168
  • Content-Type:text/html; charset=utf-8
  • Date:Thu, 09 Sep 2010 17:22:29 GMT
  • Server:Microsoft-IIS/6.0
  • X-Aspnet-Version:2.0.50727
  • X-Powered-By:ASP.NET

当我调查我编写的 CURL 脚本不知何故不会生成表单数据请求。请求方法也没有设置为 POST。在我看来,这就是出问题的地方,但不知道。

任何帮助表示赞赏!

编辑:我忘了提及,抓取的结果是远程网站的自定义会话过期页面。

For the past few days I have been trying to scrape a website but so far with no luck.

The situation is as following:
The website I am trying to scrape requires data from a form submitted previously. I have recognized the variables that are required by the web app and have investigated what HTTP headers are sent by the original web app.

Since I have pretty much zero knowledge in ASP.net, thought I'd just ask whether I am missing something here.

I have tried different methods (CURL, get contents and the Snoopy class), here's my code of the curl method:

<?php
$url = 'http://www.urltowebsite.com/Default.aspx';
$fields = array('__VIEWSTATE' => 'averylongvar',
                '__EVENTVALIDATION' => 'anotherverylongvar',
                'A few' => 'other variables');

$fields_string = http_build_query($fields);

$curl = curl_init($url);

curl_setopt_array
(
    $curl,
    array
    (
        CURLOPT_RETURNTRANSFER  =>    true,
        CURLOPT_SSL_VERIFYPEER  =>    0,  //    Not supported in PHP
        CURLOPT_SSL_VERIFYHOST  =>    0,  //        at this time.
        CURLOPT_HTTPHEADER      =>
            array
            (
                'Content-type: application/x-www-form-urlencoded; charset=utf-8',
                'Set-Cookie: ASP.NET_SessionId='.uniqid().'; path: /; HttpOnly'
            ),
        CURLOPT_POST            =>    true,
        CURLOPT_POSTFIELDS      =>    $fields_string,
        CURLOPT_FOLLOWLOCATION => 1
    )
);

$response = curl_exec($curl);
curl_close($curl);

echo $response;
?>

The following headers were requested:

Request Headers

  • Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5
  • Content-Type:application/x-www-form-urlencoded
  • User-Agent:Mozilla/5.0 (Macintosh; U;
    Intel Mac OS X 10_6_4; en-us)
    AppleWebKit/533.18.1 (KHTML, like
    Gecko) Version/5.0.2 Safari/533.18.5

Form Data

  • A lot of form fields

Response Headers

  • Cache-Control:private
  • Content-Length:30168
  • Content-Type:text/html; charset=utf-8
  • Date:Thu, 09 Sep 2010 17:22:29 GMT
  • Server:Microsoft-IIS/6.0
  • X-Aspnet-Version:2.0.50727
  • X-Powered-By:ASP.NET

When I investigate the headers of the CURL script that I wrote, somehow does not generate the Form data request. Neither is the request method set to POST. This is where it seems to me where things go wrong, but dunno.

Any help is appreciated!!!

EDIT: I forgot to mention that the result of the scraping is a custom session expired page of the remote website.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2024-09-26 07:17:35

由于 __VIEWSTATE__EVENTVALIDATION 是基本 64 字符数组,因此我对这些字段使用了 urlencode()

$fields = array('__VIEWSTATE' => urlencode( $averylongvar ),
                '__EVENTVALIDATION' => urlencode( $anotherverylongvar),
                'A few' => 'other variables');

并且对我来说效果很好。

Since __VIEWSTATE and __EVENTVALIDATION are base 64 char arrays, I've used urlencode() for those fields:

$fields = array('__VIEWSTATE' => urlencode( $averylongvar ),
                '__EVENTVALIDATION' => urlencode( $anotherverylongvar),
                'A few' => 'other variables');

And worked fine for me.

仅冇旳回忆 2024-09-26 07:17:35

由于 VIEWSTATE 包含特定情况下的页面状态(并且所有这些状态都被编码成一个大的、显然混乱的字符串),因此您无法确定您正在抓取的参数对于您的“模拟”请求是否相同(我很确定它不可能相同;))。

如果您确实必须处理 VIEWSTATE 和 EVENTVALIDATION 参数,我的建议是采用另一种方法,即通过 Selenium 或类似 HtmlUnit 的库来抓取内容(但不幸的是,我不知道 PHP 中是否有类似的东西)。

Since VIEWSTATE contains the state of the page in a particular situation (and all this state is encoded into a big, apparently messy, string), you cannot be sure that the param you are scraping can be the same for your "mock" request (I'm quite sure that it cannot be the same ;) ).

If you really have to deal with VIEWSTATE and EVENTVALIDATION params my advice is to follow another approach, that is to scrape content via Selenium or with an HtmlUnit like library (but unfortunately I don't know if there's something similar in PHP).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文