如何使用 PHP 从网站获取文本? [英] How do I get text from a website using PHP?

查看:25
本文介绍了如何使用 PHP 从网站获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在编写一个 PHP 脚本,其中一部分需要能够查询网站,然后从中获取文本.

So, I'm working on a PHP script, and part of it needs to be able to query a website, then get text from it.

首先,我需要能够查询某个网站 URL,然后我需要能够在查询后从该网站的文本中获取文本,并且能够从函数中返回该文本.

First off, I need to be able to query a certain website URL, then I need to be able to get text from the text from that website after the query, and be able to return that text out of the function.

如何查询网站并从中获取文本?

How would I query the website and get the text from it?

推荐答案

最简单的方法:

file_get_contents()

这将为您提供网页的来源.

That will get you the source of the web page.

不过,您可能想要更完整的内容,因此请查看 cURL,为了更好的错误处理,以及设置用户代理等等.

You probably want something a bit more complete though, so look into cURL, for better error handling, and setting user-agent, and what not.

从那里开始,如果您只想要文本,您将不得不解析页面.为此,请参阅:您如何在 PHP 中解析和处理 HTML/XML?

From there, if you want the text only, you are going to have to parse the page. For that, see: How do you parse and process HTML/XML in PHP?

这篇关于如何使用 PHP 从网站获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆