使用Android studio帮我写一个使用jsoup爬取网站https://www.ecook.cn/shicai/11530/中食谱图片、食谱名称和食谱描述的内容,并且用列表储存
时间: 2023-12-04 16:02:51 浏览: 121
好的,以下是使用Jsoup爬取网站并获取食谱图片、食谱名称和食谱描述的代码:
1. 首先,在build.gradle文件中添加Jsoup库的依赖:
```
implementation 'org.jsoup:jsoup:1.14.2'
```
2. 在MainActivity.java文件中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private ArrayList<Recipe> recipeList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recipeList = new ArrayList<>();
new FetchRecipesTask().execute();
}
private class FetchRecipesTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
Document doc = Jsoup.connect("https://www.ecook.cn/shicai/11530/").get();
Elements recipeElements = doc.select(".recipe-list > li > a");
for (Element recipeElement : recipeElements) {
String imgUrl = recipeElement.select(".recipe-img img").attr("data-src");
String name = recipeElement.select(".recipe-name").text();
String description = recipeElement.select(".recipe-desc").text();
Recipe recipe = new Recipe(imgUrl, name, description);
recipeList.add(recipe);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
RecipeAdapter adapter = new RecipeAdapter(recipeList);
recyclerView.setAdapter(adapter);
}
}
}
```
3. 创建Recipe类,用于储存食谱图片、食谱名称和食谱描述的内容:
```java
public class Recipe {
private String imgUrl;
private String name;
private String description;
public Recipe(String imgUrl, String name, String description) {
this.imgUrl = imgUrl;
this.name = name;
this.description = description;
}
public String getImgUrl() {
return imgUrl;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}
```
4. 创建RecipeAdapter类,用于显示食谱列表:
```java
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {
private ArrayList<Recipe> recipeList;
public RecipeAdapter(ArrayList<Recipe> recipeList) {
this.recipeList = recipeList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Recipe recipe = recipeList.get(position);
Glide.with(holder.itemView.getContext()).load(recipe.getImgUrl()).into(holder.imageView);
holder.nameTextView.setText(recipe.getName());
holder.descriptionTextView.setText(recipe.getDescription());
}
@Override
public int getItemCount() {
return recipeList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView nameTextView;
TextView descriptionTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
nameTextView = itemView.findViewById(R.id.nameTextView);
descriptionTextView = itemView.findViewById(R.id.descriptionTextView);
}
}
}
```
5. 在res/layout文件夹中创建recipe_item.xml文件,用于定义食谱列表项的布局:
```xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="96dp"
android:layout_height="96dp"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
```
这样,就可以获取网站https://www.ecook.cn/shicai/11530/中的食谱图片、食谱名称和食谱描述,并用列表储存并显示出来了。
阅读全文