如何通过DOM验证年龄验证 [英] How to pass Age Verification with DOM

查看:249
本文介绍了如何通过DOM验证年龄验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

http://store.steampowered.com/app/252490/

I'm attempting to pull some image URLs from Steam store pages, such as: 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');
}

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

It works fine with the first store page, but the second one redirects to an age verification page, and the script returns the images from there. I need a way for the script to get past the age verification and access the actual store page.

任何帮助都会感激。

编辑:

这是提交年龄表时传递给服务器的信息:

This is what's passed to the server when the age form is submitted:

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

及其设置的cookie:

and the cookies that it sets:

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

Edit2:

我可以使用cURL设置Cookie,但它们不会被DOM加载HTML使用,因此我获得的结果与之前相同。我需要一个方法loadHTML使用我设置的特定的cookie,或另一种方法抓住图像URL,将使用由cURL设置的cookie。

I can set the cookies using cURL but they aren't used by DOM loadHTML, so I get the same result as before. I need either a way for loadHTML to use specific cookies that I set, or another method of grabbing the image URLs that will use cookies set by cURL.

推荐答案

解决了!这里是工作代码:

Solved! Here's the working code:

$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);

这篇关于如何通过DOM验证年龄验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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