Python Requests - response.textWhen working with Python's requests library, we often make HTTP requests to specific URIs (Uniform Resource Identifiers). These requests return a response object that contains various properties and methods to interact with the data received from the server. One of these properties is response.text. It provides the content of the response in Unicode format. Essentially, it refers to the binary response content decoded into human-readable text. When you request a specified URI through Python, it returns this response object. Here's how you can use response.text in Python requests with some examples: Fetching Content from a URLSuppose you want to retrieve the HTML content of a webpage. You can use response.text to access the text representation of the response: Program Output {"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}"} Explanation In the above Python code, we imported the 'requests' library to send an HTTP GET request to the URL 'https://www.javatpoint.com'. Upon receiving a response from the server, it stores the response object in the variable 'response'. Finally, it prints the response, including the webpage's HTML content at the specified URL. Parsing JSON DataIf the server responds with JSON data, you can use response.text to extract and work with it: Example Output Apple Explanation In the above-provided code, we fetched JSON data from a URL, extracted it, and printed a specific key's value from the JSON. Searching for PatternsYou can search for specific patterns or keywords within the response text using regular expressions: Example Output Found the phrase: javatpoint Explanation The above code searches for the phrase "Python is awesome" in the HTML content of a webpage fetched from a URL. It prints a message indicating that the phrase was found if it finds it. Several libraries are available in Python for making HTTP requests, such as treq, httplib, urllib, and httplib2, but requests are the best and have the most capabilities. Use the property below to verify the status code if any requested attribute displays NULL. ConclusionThe response.text attribute is a powerful way to retrieve content from an HTTP response. It allows us to get the raw text of the response, basically HTML or plain text. Whether you're creating web data, consuming APIs, or interacting with web services, response.text provides an easy way to extract and manipulate text data in our systems. This method of convenience is multi-tasking, which is key to processing HTTP responses in multiple web-related systems using Python. Next TopicRetrieve elements from python set |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India