今年のトップ100の音楽を収集するウェブスクレイパーを作ろうとしています。現時点では、私はソースコードを収集する関数を記述しようとしています。私はちょうどコピーして、他のスクレーパーからそれを貼り付けましたが、何らかの奇妙な理由でそれは空のリストを返します。ソースコードの取得//ファイルリーダー//空のリストの返信
私は関数get_source_codeを使って作業していると思いますが、間違っている可能性があります。エラーメッセージは返されません。おかげさまで、よろしくお願い申し上げます。
import java.util.ArrayList;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
public class MusicScraper {
public static void main(String [] args)throws IOException {
parse_source_code(get_source_code("","",""));
}
public static List<String> get_source_code(String day, String month, String year)throws IOException{
List <String> sourceC = new ArrayList<>();
URL link = new URL("https://www.billboard.com/charts/hot-100/2017-02-25"); //"http://www.billboard.com/charts/hot-100/" + year + "-" + month + "-" + day);
HttpsURLConnection billboardConnection = (HttpsURLConnection) link.openConnection();
billboardConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
billboardConnection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(billboardConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
sourceC.add(inputLine);
}
System.out.println(sourceC);
return sourceC;
}
public static List<String> parse_source_code(List<String> sourceCode){
List<String> data = new ArrayList<>();
List<String> rank = new ArrayList<>();
List<String> song = new ArrayList<>();
List<String> artist = new ArrayList<>();
for (int i = 0; i < sourceCode.size(); i++) {
if (sourceCode.get(i).contains("data-songtitle=\"")) {
String parsedSong = sourceCode.get(i).split("data-songtitle=\"")[1].split("\">")[0];
song.add(parsedSong);
}
}
System.out.println(song);
return sourceCode;
}
}
これは意味があります。ありがとうございます。それは私が追加または削除する必要があるコードで指定されているようにhttpsではないので、今私は新しいエラーメッセージを受け取る意味があります。 – Jblue
感謝してくれてありがとうございました。 – Jblue
@ Jblueあなたは 'HttpsURLConnection'の代わりに' HttpURLConnection'を使いたいと思っています。 – explv