我已经在php
中编写了一个脚本来从网页获取html内容或源代码,但是我无法成功。当我执行脚本时,它会打开页面本身。如何获取html元素或源代码?
这是脚本:
<?php
include "simple_html_dom.php";
function get_source($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$htmlContent = curl_exec($ch);
curl_close($ch);
$dom = new simple_html_dom();
$dom->load($htmlContent);
return $dom;
}
$scraped_page = get_source("https://stackoverflow.com/questions/tagged/web-scraping");
echo $scraped_page;
?>
当前,我的状态如下: