【发布时间】:2021-02-16 00:13:21
【问题描述】:
我有一个有效的代码
$html = file_get_contents('https://www.imdb.com/');
echo $html;
这个代码也可以工作
$html = file_get_contents('https://www.google.com/');
echo $html;
但它不适用于像这样的一些网址:
$html = file_get_contents('https://www.rottentomatoes.com/');
echo $html;
我得到了这个错误
Warning: file_get_contents(https://www.rottentomatoes.com/tv/friends): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
我不明白为什么会这样? 并且网站在浏览器中加载没有问题,也不需要 vpn(但可能需要其他一些 url)。
我还使用了 Simple HTML DOM Parser,在 1.9 版本中我得到了这个错误,在 2.0RC2 版本中我得到了空的 $html 和 NULL 值。
有人可以帮帮我吗?
【问题讨论】:
-
@berend 有点类似,但他们的回答是靠运气!
-
网页抓取可能是一项复杂的任务,具体取决于站点及其为防止抓取而实施的解决方案,在可能的情况下:某些站点可能需要一些 cookie 作为保护(可以通过 curl 启用它们),他们还可以检查请求是否来自浏览器,只允许某些方法(例如 POST 或 GET),或者如果他们使用 JS 框架渲染页面,您可能会得到不完整的结果。添加细节可以帮助人们正确回答你
-
@Kaddath 感谢您提供有用的信息。在继续之前,我有点想知道为什么这个完全相同的代码适用于我在另一个国家的朋友,而不是 1 每次他运行它而不为我工作?也许取决于互联网连接?我不知道
-
@Kaddath 谢谢你,我会试试的,但没有太多关于网络抓取烂番茄的信息
标签: php web-scraping simple-html-dom