如何解决无法使用 jsoup 获取准确的图像和文本
我想从以下网站获取图像 URL 和标题。我到处搜索并尝试了许多 jsoup 代码,但我无法获得确切的图像和标题。我已在清单文件中添加了 Internet 权限并添加了依赖项。
https://www.forbesindia.com/lists/2015-celebrity-100/1519/1
到目前为止我已经尝试过这个
new Thread(new Runnable()
{
//final EbayProduct product = new EbayProduct();
@Override
public void run()
{
try
{
Document doc = Jsoup.connect("https://www.forbesindia.com/lists/2015-celebrity-100/1519/1").get();
Elements body = doc.getAllElements();
//System.out.println(body.select("tr").size());
// Log.i("Size",Integer.toString(body.select("td").size()));
for(Element e : body.select("img"))
{
String image = e.select(" img").attr("src");
String title = e.select("img").attr("alt");
celebURLs.add(image);
celebNames.add(title);
Log.i("bal",String.valueOf(celebURLs));
Log.i("bal2",String.valueOf(celebNames));
}
newQuestion();
}catch (Exception ex){
ex.printStackTrace();
}
}
}).start();
这会提供来自网站而非列表的随机图像。
Logcat
2021-05-04 22:46:00.904 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png]
2021-05-04 22:46:00.904 25418-25548/com.example.guessthecelebrity I/bal2: []
2021-05-04 22:46:00.904 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/list/banner/list_1519.jpg]
2021-05-04 22:46:00.905 25418-25548/com.example.guessthecelebrity I/bal2: [,2015 Celebrity 100 - Forbes India Magazine]
2021-05-04 22:46:00.905 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/list/banner/list_1519.jpg,https://www.forbesindia.com/media/sectionimage/header/30under30_21_230_20210203091422_230x62.jpg]
2021-05-04 22:46:00.905 25418-25548/com.example.guessthecelebrity I/bal2: [,2015 Celebrity 100 - Forbes India Magazine,]
2021-05-04 22:46:00.906 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/30under30_21_230_20210203091422_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/managers_230_20201013010335_230x62.jpg]
2021-05-04 22:46:00.906 25418-25548/com.example.guessthecelebrity I/bal2: [,https://www.forbesindia.com/media/sectionimage/header/managers_230_20201013010335_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/forbesindia_richlist_sc230_20201008054355_230x62.jpg]
2021-05-04 22:46:00.906 25418-25548/com.example.guessthecelebrity I/bal2: [,]
2021-05-04 22:46:00.907 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/forbesindia_richlist_sc230_20201008054355_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/wpower_2020_1_20200303061653_230x62.jpg]
2021-05-04 22:46:00.907 25418-25548/com.example.guessthecelebrity I/bal2: [,https://www.forbesindia.com/media/sectionimage/header/wpower_2020_1_20200303061653_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/30under30_section_sm_20200205082639_230x62.jpg]
2021-05-04 22:46:00.908 25418-25548/com.example.guessthecelebrity I/bal2: [,]
2021-05-04 22:46:00.908 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/30under30_section_sm_20200205082639_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/celeb19_sm_20191219030109_230x62.jpg]
2021-05-04 22:46:00.909 25418-25548/com.example.guessthecelebrity I/bal2: [,]
2021-05-04 22:46:00.909 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/celeb19_sm_20191219030109_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/richlist2019_230_20191011022510_230x62.jpg]
2021-05-04 22:46:00.909 25418-25548/com.example.guessthecelebrity I/bal2: [,]
2021-05-04 22:46:00.910 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/richlist2019_230_20191011022510_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/wpower2019_230_20190301085545_230x62.jpg]
2021-05-04 22:46:00.910 25418-25548/com.example.guessthecelebrity I/bal2: [,]
2021-05-04 22:46:00.911 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/wpower2019_230_20190301085545_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/30under30_2019_sm1_20190204072937_230x62.jpg]
2021-05-04 22:46:00.911 25418-25548/com.example.guessthecelebrity I/bal2: [,https://www.forbesindia.com/media/sectionimage/header/30under30_2019_sm1_20190204072937_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/celeb18_sm_20181205054447_230x62.jpg]
2021-05-04 22:46:00.912 25418-25548/com.example.guessthecelebrity I/bal2: [,]
2021-05-04 22:46:00.913 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/celeb18_sm_20181205054447_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/richlistsection_sm_20181004054854_230x62.jpg]
2021-05-04 22:46:00.913 25418-25548/com.example.guessthecelebrity I/bal2: [,]
2021-05-04 22:46:00.914 25418-25548/com.example.guessthecelebrity I/bal: [https://www.forbesindia.com/forbes-rvp-files/images/forbes-logo.png,https://www.forbesindia.com/media/sectionimage/header/richlistsection_sm_20181004054854_230x62.jpg,https://www.forbesindia.com/media/sectionimage/header/supper50_sm_20180821013603_230x62.jpg]
2021-05-04 22:46:00.914 25418-25548/com.example.guessthecelebrity I/bal2: [,]
我得到的是图片网址和标题,但不是 100 个名人图片网址和标题。
完整的java文件
package com.example.guessthecelebrity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
ArrayList<String> celebURLs = new ArrayList<String>();
ArrayList<String> celebNames = new ArrayList<String>();
int chosenCeleb = 0;
String[] answers = new String[4];
int locationOfCorrectAnswer = 0;
ImageView imageView;
Button button0;
Button button1;
Button button2;
Button button3;
public void celebChosen(View view) {
Log.i("bal","celebchosen");
if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))) {
Toast.makeText(getApplicationContext(),"Correct!",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"Worng! It was " + celebNames.get(chosenCeleb),Toast.LENGTH_SHORT).show();
}
newQuestion();
}
public class ImageDownloader extends AsyncTask<String,Void,Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
try {
Log.i("bal","imagedownloader");
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public class DownloadTask extends AsyncTask<String,String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
Log.i("bal","downloadTask");
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public void newQuestion() {
try {
Log.i("bal","newQuestion");
Random rand = new Random();
chosenCeleb = rand.nextInt(celebURLs.size());
ImageDownloader imageTask = new ImageDownloader();
Bitmap celebImage = imageTask.execute(celebURLs.get(chosenCeleb)).get();
imageView.setImageBitmap(celebImage);
locationOfCorrectAnswer = rand.nextInt(4);
int incorrectAnswerLocation;
for (int i = 0; i < 4; i++) {
if (i == locationOfCorrectAnswer) {
answers[i] = celebNames.get(chosenCeleb);
} else {
incorrectAnswerLocation = rand.nextInt(celebURLs.size());
while (incorrectAnswerLocation == chosenCeleb) {
incorrectAnswerLocation = rand.nextInt(celebURLs.size());
}
answers[i] = celebNames.get(incorrectAnswerLocation);
}
}
button0.setText(answers[0]);
button1.setText(answers[1]);
button2.setText(answers[2]);
button3.setText(answers[3]);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button0 = findViewById(R.id.button0);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
DownloadTask task = new DownloadTask();
String result = null;
Log.i("bal","Works");
new Thread(new Runnable()
{
//final EbayProduct product = new EbayProduct();
@Override
public void run()
{
try
{
Document doc = Jsoup.connect("https://www.forbesindia.com/lists/2015-celebrity-100/1519/1").get();
Elements body = doc.getAllElements();
//System.out.println(body.select("tr").size());
// Log.i("Size",String.valueOf(celebNames));
}
newQuestion();
}catch (Exception ex){
ex.printStackTrace();
}
}
}).start();
}
}
XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#151414"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="@+id/button0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:color/holo_blue_bright" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:onClick="celebChosen"
android:tag="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:onClick="celebChosen"
android:tag="2"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:onClick="celebChosen"
android:tag="1"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:onClick="celebChosen"
android:tag="0"
app:layout_constraintBottom_toTopOf="@+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。