php 抓取动态网页_PHP – 加载所有动态内容后获取页面内容

博主尝试从Unity Asset Store的网页中抓取并解析'TopPaid'框的内容,首先尝试了file_get_contents和cURL方法,但未能成功获取目标代码。之后,博主采用PHP WebDriver库,通过模拟浏览器行为来打开页面、设置cookie、等待元素出现,并最终获取到页面源代码。代码示例中展示了如何使用WebDriver来操作浏览器并获取页面源代码。
摘要由CSDN通过智能技术生成

我尝试获取此页面的源代码:

https://www.assetstore.unity3d.com/en/

我想在右侧解析一个小项目的“Top Paid”框,但是当我使用file_get_contents或以下代码时,我没有得到正确的源代码.

$cookie = tmpfile();

$userAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' ;

$ch = curl_init('https://www.assetstore.unity3d.com/en/');

$options = array(

CURLOPT_CONNECTTIMEOUT => 20 ,

CURLOPT_USERAGENT => $userAgent,

CURLOPT_AUTOREFERER => true,

CURLOPT_FOLLOWLOCATION => true,

CURLOPT_RETURNTRANSFER => true,

CURLOPT_COOKIEFILE => $cookie,

CURLOPT_COOKIEJAR => $cookie ,

CURLOPT_SSL_VERIFYPEER => 0 ,

CURLOPT_SSL_VERIFYHOST => 0 ,

CURLOPT_TIMEOUT => 10

);

curl_setopt_array($ch, $options);

$kl = curl_exec($ch);

curl_close($ch);

echo $kl;

?>

返回:

但Top Paid Box位于“mainContent”部分.我该如何获得此代码?

解决了

感谢Pramod,这是我的代码:

// An example of using php-webdriver.

require_once('lib/__init__.php');

// start Firefox with 5 second timeout

$host = 'http://localhost:4444/wd/hub'; // this is the default

$capabilities = DesiredCapabilities::firefox();

$driver = RemoteWebDriver::create($host, $capabilities, 5000);

// navigate to 'http://docs.seleniumhq.org/'

$driver->get('https://www.assetstore.unity3d.com/en/');

// adding cookie

$driver->manage()->deleteAllCookies();

$driver->manage()->addCookie(array(

'name' => 'cookie_name',

'value' => 'cookie_value',

));

$cookies = $driver->manage()->getCookies();

// wait at most 10 seconds until at least one result is shown

$driver->wait(10)->until(

WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(

WebDriverBy::className('top-list')

)

);

$sString = $driver->getPageSource();

// close the Firefox

$driver->quit();

print_r($sString);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值