Python Requests - response.text

2025年1月5日 | 阅读 2 分钟

在使用 Python 的 requests 库时,我们经常向特定的 URI(统一资源标识符)发起 HTTP 请求。这些请求会返回一个响应对象,该对象包含各种属性和方法,用于与服务器返回的数据进行交互。

Python Requests - response.text

其中一个属性是 response.text。它以 Unicode 格式提供响应内容。本质上,它指的是已解码为人类可读文本的二进制响应内容。当您通过 Python 请求指定的 URI 时,它会返回此响应对象。以下是如何在 Python requests 中使用 response.text 的一些示例:

获取 URL 内容

假设您想检索网页的 HTML 内容。您可以使用 response.text 来访问响应的文本表示。

程序

输出

{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":"https://api.github.com/search/code?q={query}{&page,per_page,sort,order}","commit_search_url":"https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}","emails_url":"https://api.github.com/user/emails","emojis_url":"https://api.github.com/emojis","events_url":"https://api.github.com/events","feeds_url":"https://api.github.com/feeds","followers_url":"https://api.github.com/user/followers","following_url":"https://api.github.com/user/following{/target}","gists_url":"https://api.github.com/gists{/gist_id}","hub_url":"https://api.github.com/hub","issue_search_url":"https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}","issues_url":"https://api.github.com/issues","keys_url":"https://api.github.com/user/keys","label_search_url":"https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}","notifications_url":"https://api.github.com/notifications","organization_url":"https://api.github.com/orgs/{org}","organization_repositories_url":"https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}","organization_teams_url":"https://api.github.com/orgs/{org}/teams","public_gists_url":"https://api.github.com/gists/public","rate_limit_url":"https://api.github.com/rate_limit","repository_url":"https://api.github.com/repos/{owner}/{repo}","repository_search_url":"https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}","current_user_repositories_url":"https://api.github.com/user/repos{?type,page,per_page,sort}","starred_url":"https://api.github.com/user/starred{/owner}{/repo}","starred_gists_url":"https://api.github.com/gists/starred","topic_search_url":"https://api.github.com/search/topics?q={query}{&page,per_page}","user_url":"https://api.github.com/users/{user}","user_organizations_url":"https://api.github.com/user/orgs","user_repositories_url":"https://api.github.com/users/{user}/repos{?type,page,per_page,sort}","user_search_url":"https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"}

说明

在上面的 Python 代码中,我们导入了“requests”库,向 URL“https://tpointtech.cn”发送了一个 HTTP GET 请求。从服务器收到响应后,它将响应对象存储在名为“response”的变量中。最后,它打印了响应,包括指定 URL 的网页的 HTML 内容。

解析 JSON 数据

如果服务器以 JSON 数据响应,您可以使用 response.text 来提取并处理它。

示例

输出

Apple

说明

在上提供的代码中,我们从 URL 获取了 JSON 数据,提取了它,并打印了 JSON 中特定键的值。

搜索模式

您可以使用正则表达式在响应文本中搜索特定模式或关键字。

示例

输出

Found the phrase: javatpoint

说明

上面的代码在从 URL 获取的网页的 HTML 内容中搜索短语“Python is awesome”。如果找到该短语,它会打印一条消息,表明已找到该短语。

Python 中有多种可用于发起 HTTP 请求的库,例如 treq、httplib、urllib 和 httplib2,但 requests 是最好的,功能也最强大。如果任何请求的属性显示为 NULL,请使用以下属性来验证状态码。

结论

response.text 属性是检索 HTTP 响应内容的强大方法。它允许我们获取响应的原始文本,基本上是 HTML 或纯文本。无论您是进行网络数据抓取、使用 API 还是与 Web 服务交互,response.text 都提供了一种简便的方法来提取和操作系统中的文本数据。这种便捷的方法是多任务的,这是使用 Python 处理多个 Web 相关系统中的 HTTP 响应的关键。