How to Convert a Website URL Page to Text using Java
Reviewing website copy can be a time-consuming task, requiring one to read carefully through each of the many separate pages that constitute a typical client-facing site. Thankfully, however, there’s an easy way around that — our URL to Text conversion API makes will quickly strip all available text from an input URL page’s HTML code, returning a string containing only plain text (“TextContentResult”) and no additional clutter. The best part? You can use this API for free by registering a free account on our website. Free-tier accounts are perfect for smaller-scale projects & come with zero additional commitments, supplying a hard-and-fast limit of 800 API calls per month. Once you’ve registered your account, you can follow instructions below to install the client & structure your API call in Java.
We can begin by installing the API client with Maven. First, let’s include a reference in the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, we can add a reference to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Moving on to our controller, we can add our imports to the top of our file:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertWebApi;
We can now copy in the final snippet below & include our API key below the “configure API key authorization” comment. Simply include your URL in the request parameter format, and you’re all set:
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");ConvertWebApi apiInstance = new ConvertWebApi();
UrlToTextRequest input = new UrlToTextRequest(); // UrlToTextRequest | HTML to Text request parameters
try {
UrlToTextResponse result = apiInstance.convertWebUrlToTxt(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertWebApi#convertWebUrlToTxt");
e.printStackTrace();
}
Request parameter format:
{
"Url": "string"
}