【问题标题】:How to pass Age Verification with DOM如何使用 DOM 通过年龄验证
【发布时间】:2014-04-04 02:43:33
【问题描述】:

我正在尝试从 Steam 商店页面中提取一些图片 URL,例如: http://store.steampowered.com/app/35700/
http://store.steampowered.com/app/252490/

这是我正在使用的代码:

$url = 'http://store.steampowered.com/app/35700/';
$html = file_get_contents($url);
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
  echo $image->getAttribute('src');
}

它适用于第一个商店页面,但第二个重定向到年龄验证页面,脚本从那里返回图像。我需要一种方法让脚本通过年龄验证并访问实际的商店页面。

任何帮助将不胜感激。

编辑:

这是提交年龄表单时传递给服务器的内容:

snr=1_agecheck_agecheck__age-gate&ageDay=1&ageMonth=January&ageYear=1979

以及它设置的 cookie:

lastagecheckage=1-January-1979; expires=Tue, 03 Mar 2015 19:53:42 GMT; path=/; domain=store.steampowered.com
birthtime=662716801; path=/; domain=store.steampowered.com

编辑2:

我可以使用 cURL 设置 cookie,但 DOM loadHTML 不使用它们,所以我得到与以前相同的结果。我需要一种方法让 loadHTML 使用我设置的特定 cookie,或者另一种获取图像 URL 的方法,该方法将使用 cURL 设置的 cookie。

【问题讨论】:

  • 我想你传递了一个特定的值,只要它不是动态的,你就可以通过 firebug 等挖掘它,然后你的 api 调用被允许跟随重定向。
  • 我找到了年龄检查表的代码,以及它设置的 cookie,但我不知道从那里去哪里。 (用信息编辑主帖)
  • 您可能需要使用 cURL 之类的东西来处理获取 cookie,然后将它们存储起来以在第二个请求中使用。
  • 我可以使用 cURL 设置 cookie,但我不知道如何允许 loadHTML 请求使用它们。

标签: php image dom cookies curl


【解决方案1】:

解决了!这是工作代码:

$url = 'http://store.steampowered.com/app/35700/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_COOKIE, "birthtime=28801; path=/; domain=store.steampowered.com");
curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

$dom = new domDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($result);
$dom->preserveWhiteSpace = false;

$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
  $src = $image->getAttribute('src');
  echo $src.PHP_EOL;
}

curl_close($ch);

【讨论】:

    【解决方案2】:

    您正在寻找 php 答案,但我试图在 python 中做同样的事情,这是最相关的问题。您的 php 答案帮助了我,所以也许 python 解决方案会帮助某人。我在 Python 2.7 中使用 python-requests 的解决方案:

        import requests
    
        url = 'http://store.steampowered.com/app/252490/'
        cookie = {
                'birthtime' : '28801',
                'path' : '/',
                'domain' : 'store.steampowered.com'
                }
    
        r = requests.get(url, cookies=cookie)
        assert (r.status_code == 200 and r.text.find('Please enter your birth date to continue') < 0), ("Failed to retrieve page for {url}. Error={code}.".format(url=url, code=r.status_code))
    
        print r.text.encode('utf-8')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2013-04-10
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      相关资源
      最近更新 更多