php - 从网页获取源代码时遇到问题
问题描述
我已经编写了一个脚本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;
?>
目前我是这样的:
解决方案
推荐阅读
- python-3.x - 查找给定列表的重叠
- c++ - 如何访问 C 文件中的 C++ public bool 类成员(以验证)?
- python - matplotlib 弹出窗口中的错误(AttributeError:'NoneType' 对象没有属性'set_canvas')
- python-3.x - 将获取请求从树莓派重新发送到烧瓶应用程序时阻止输入
- angular - Ionic 4中的动态进度条?
- c# - 在 MVC 中追加重复的 div 时不出现下拉列表
- python - 使用 PyInstaller 将 Cython 编译的模块和 python 代码构建为可执行二进制文件
- android - 根据方位旋转谷歌地图,中心有固定标记
- php - 文本区域中的双中继器克隆内容
- swift - 线程 1:致命错误:在展开可选值 (setupInputOutput) 时意外发现 nil