Last active
January 11, 2016 04:32
-
-
Save elvismdev/ad5f3ccf8c64e6b14e03 to your computer and use it in GitHub Desktop.
A Java small class to read the lines from a given URL location/webpage and print each URL that is a link from another defined website. The library edu.duke is a dependency for the class to work, it should be added into the Java IDE to compile with no errors. Download link http://www.dukelearntoprogram.com/downloads/archives/courserajava.jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Read the lines from a given URL location and prints each URL that is a link from a defined website. | |
* E.g. All links matching youtube.com. | |
* | |
* @author (Elvis Morales) | |
* @version (1.0) | |
*/ | |
import edu.duke.*; | |
import java.io.*; | |
public class FindWebLinks { | |
public void findLinks() { | |
URLResource webPage = new URLResource("http://www.billboard.com/articles/news/6836078/youtube-fan-uploaded-music-videos-2015"); | |
for ( String word : webPage.words() ) { | |
String wLow = word.toLowerCase(); | |
if ( wLow.contains( "youtube.com" ) && ( wLow.contains( "https://" ) || wLow.contains( "http://" ) ) ) { | |
int startQuote = wLow.indexOf("\""); | |
int endQuote = wLow.lastIndexOf("\""); | |
String ytLink = word.substring( startQuote+1, endQuote ); | |
System.out.println( ytLink ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment