我正在使用$ html = file_get_html($ url) - > plaintext;从网站上获取纯文本。但是如果我在某个循环中运行此代码,因为我想从一系列网站访问文本,那么对于少数网站,它会抛出错误,但如果我专门访问此功能,则会提取相同网站的纯文本那个网站只有
答案 0 :(得分:0)
尝试一下,让我知道我是否在你想要的正确或错误的轨道上。首先,我在服务器上有2个html文件:
→page1.html
<html>
<head><title>HTML Page #1</title></head>
<body><div id="a">A1</div><div id="b">B1</div><div id="b">B1</div></body>
</html>
→page2.html
<html>
<head><title>HTML Page #2</title></head>
<body><div id="a">A2</div><div id="b">B2</div><div id="b">B2</div></body>
</html>
→好的,然后是主PHP页面 :(与.html文件在同一文件夹中)
require_once( 'simple_html_dom.php' ); // or w\e the path for your source is
// array to store series of websites:
$url = array( './page1.html', './page2.html' );
// array to hold plain-text for later retrieval: (if needed)
$html = array();
// loop through series of webpages:
foreach( $url as $key=>$value ) {
// get html plain-text for webpage & assign to html array.
$html[ $key ] = file_get_html( $value )->plaintext;
// echo html plain text:
echo "<br />" . $html[ $key ] . "<br />";
}
// echo $html[ 0 ]; /* <-- page1.html (if needed) */
// echo $html[ 1 ]; /* <-- page2.html (if needed) */
→输出:
HTML Page #1 A1B1B1
HTML Page #2 A2B2B2
→这是否达到了您希望代码执行的操作?