62.
public class MainActivity extends AppCompatActivity {
private List<String> strIDList;
private List<Document> docsList;
private List<KugouMusicInfo> kugouMusicInfosList;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
visitKugou();
textView = (TextView) findViewById(R.id.text1);
}
private List<String> strIDList;
private List<Document> docsList;
private List<KugouMusicInfo> kugouMusicInfosList;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
visitKugou();
textView = (TextView) findViewById(R.id.text1);
}
63.
public void visitKugou(){
//创建线程
new Thread(){
@Override
public void run() {
super.run();
//定义path
String path = "http://m.kugou.com";
try {
Document doc = Jsoup.connect(path).userAgent("Mozilla/5.0 (Linux;" +
" Android 6.0;" +
" Nexus 5 Build/MRA58N) AppleWebKit/537.36" +
" (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36").
timeout(5000).
get();
//设置选择器,查找li标签中的id属性
Elements liID = doc.select("li[id]");
//测试输出
System.out.println(String.format("li:%d", liID.size()));
//使用list保存提取出来的strID
strIDList = new ArrayList<>();
for (Element li : liID){
if (li.tagName().equals("li")){
System.out.println("<li>" + String.format("%s:%s",
li.tagName(),
li.attr("id")));
String strID = li.attr("id");
int begin = strID.indexOf("_") + 1;
strID = strID.substring(begin, 38);
System.out.println(strID);
strIDList.add(strID);
}
}
//创建线程
new Thread(){
@Override
public void run() {
super.run();
//定义path
String path = "http://m.kugou.com";
try {
Document doc = Jsoup.connect(path).userAgent("Mozilla/5.0 (Linux;" +
" Android 6.0;" +
" Nexus 5 Build/MRA58N) AppleWebKit/537.36" +
" (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36").
timeout(5000).
get();
//设置选择器,查找li标签中的id属性
Elements liID = doc.select("li[id]");
//测试输出
System.out.println(String.format("li:%d", liID.size()));
//使用list保存提取出来的strID
strIDList = new ArrayList<>();
for (Element li : liID){
if (li.tagName().equals("li")){
System.out.println("<li>" + String.format("%s:%s",
li.tagName(),
li.attr("id")));
String strID = li.attr("id");
int begin = strID.indexOf("_") + 1;
strID = strID.substring(begin, 38);
System.out.println(strID);
strIDList.add(strID);
}
}
64.
public class MainActivity extends AppCompatActivity {
private TextView textView;
private List<String> content = new ArrayList<>();
//http://m.kugou.com/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_show);
textView.setText("抓取中...");
runSubThread();
}
public void runSubThread(){
new Thread(){
@Override
public void run() {
super.run();
String path = "http://m.kugou.com";
try {
Document doc = Jsoup.connect(path).userAgent("Mozilla/5.0 (Linux;" +
" Android 6.0;" +
" Nexus 5 Build/MRA58N) AppleWebKit/537.36" +
" (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36").
timeout(5000).
get();
Elements links = doc.select("a[href]");
Elements media = doc.select("img[src]");
Elements imports = doc.select("link[href]");
System.out.println(String.format("Media:(%d)", media.size()));
for (Element src : media)
{
if (src.tagName().equals("img")){
System.out.println("<img>" + String.format("%s:%s",
src.tagName(),
src.attr("abs:src")));
content.add("<img>" + String.format("%s:%s",
src.tagName(),
src.attr("abs:src")));
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < content.size(); i++)
textView.setText(content.get(i));
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}