如何通过网址获取网页上的文字(需要一些想法或建议)?

时间:2016-11-28 11:11:13

标签: javascript php

如何通过网址获取网页上的文字(需要一些想法或建议)?

通常我使用此代码使用url获取文件数据

<?PHP
$text = file_get_contents('https://www.example.com/sometextfile.txt');
echo $text;
?>

现在我想使用网址获取网页上的所有文字。

我可以通过PHP或JavaScript来实现吗?

2 个答案:

答案 0 :(得分:0)

试试PHP Simple HTML DOM Parser。它使用类似jQuery的选择器,非常整洁。

答案 1 :(得分:0)

如果您尝试从外部URL访问文本,那么您可以使用带有strip_tags的file_get_contents函数:

<?PHP
$text = file_get_contents('https://www.example.com/sometextfile.txt');
echo strip_tags($text); // If you only need text not html structure
?>

另一种PHP方式是CURL。您可以启用php_curl并使用curl方法执行此操作(http://php.net/manual/en/curl.examples.php)。