Utilice el paquete Jsoup para obtener información de la película Douban Top250

La herramienta de análisis de páginas web de Java Jsoup se utiliza principalmente para hacer programas de rastreo, mientras que en Python es una sopa hermosa. Jsoup puede obtener el modelo (dom) de archivos de páginas web como JS y JQuery. Es una herramienta poderosa para analizar archivos web. Vea cómo para usarlo: Jsoup detallado (1) -Jsoup detallado

El uso de Jsoup es básicamente el mismo que el de JS, ingrese a la interfaz Douban Top250:


Encuentra la dirección del enlace:

String[] url=new String[25];
		//进入Top250的界面,抓取单个电影的网址
		Document document=Jsoup.connect("https://movie.douban.com/top250?start="+j*25).get();
        Elements bd=document.select("div.hd");
        for(int i=0;i<bd.size();i++)
        {
    
    
        	Elements info=bd.get(i).select("a[href]");
        	url[i]=info.attr("href");
        }
        return url;

En esta sección, necesitamos una función para obtener toda la información de la página web después de pasar la URL a esta función.
Si el texto está dentro de la etiqueta, después de seleccionar esta etiqueta, el método de texto puede extraer el texto. Si no lo está, por ejemplo, antes de <br>, dicho contenido pertenece al nodo de texto, puede buscar el nodo vecino y luego use nextSibling () y otros métodos Obtenga el nodo de texto. Cabe señalar que:
1. Documento: modelo de objeto de documento (dom)
2. Elementos: colección de objetos de elemento
3. Nodo: objetos de nodo
Estos tres están ordenados de grandes a pequeños. Los objetos de nodo normales no tienen método de texto. Utilice el outhtml método para obtenerlo. El valor, excepto para los nodos de texto.

//抓取电影名
		String name=document.select("span[property=v:itemreviewed]").text();
		info[0]=name;
		//抓取导演名
		String director=document.select("a[rel='v:directedBy']").text();
		info[1]=director;
		//抓取编剧
		String pl=document.select("span.attrs").get(1).text();
		info[2]=pl;
		//抓取演员名
		String actor=document.select("span.attrs").get(2).text();
		info[3]=actor;
		//抓取电影类型
		String type=document.select("span[property='v:genre']").text();
		info[4]=type;
		//对是否含有官方网站进行讨论
		int i=4;
		if(document.select("span.pl").get(i).text().contains("官方网站"))
		{
    
    
			i++;
		}
		//抓取产地国家,它的内容是国家标签的下一个节点
		String country=document.select("span.pl").get(i).nextSibling().outerHtml();
		info[5]=country;
		//抓取语言
		String lan=document.select("span.pl").get(i+1).nextSibling().outerHtml();
		info[6]=lan;
		//抓取时长
		String runTime=document.select("span[property='v:runtime']").text();
		info[7]=runTime;
		//抓取别名
		String otherName=document.select("span.pl").get(i+4).nextSibling().outerHtml();
		info[8]=otherName;
		//抓取评价人数
		String peoNum=document.select("span[property='v:votes']").text();
		info[9]=peoNum;
		//抓取介绍
		String intro=document.getElementById("link-report").text();
		info[10]=intro;

Aquí está el proceso de obtener información de la película Douban.




Después de tomar la información, la ponemos en una tabla. Aquí está el paquete poi. Toma la puerta para procesar documentos, docx, xlsx, etc.

private static void writeRow(int i ,String[] row,File file) throws InvalidFormatException, IOException
	{
    
    
		Workbook workBook=new XSSFWorkbook(new FileInputStream(file));
		//获得Top250Sheet,前提是有这个sheet,没有会报错
		Sheet sheet=workBook.getSheet("Top250");
		Row r=sheet.createRow(i);
		for(int k=0;k<row.length;k++)
		{
    
    
			r.createCell(k).setCellValue(row[k]);
		}
		//将数据写进表格
		workBook.write(new FileOutputStream(file));
		
	}

Recorra cada página para obtener las URL de todas las películas. El
código total es el siguiente

package spider;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class DoubanTop250 
{
    
    
	
	public static void main(String[] args) throws InterruptedException, IOException, InvalidFormatException 
	{
    
    
		File file=new File("Top250.xlsx");
		//写好表格标题
		String[] title= {
    
    "电影名","导演","编剧","演员","类型","国家","语言","时长","别名","评价人数","简介"};
		writeRow(0,title,file);
		//遍历,抓取Top250电影信息
		for(int k=0	;k<10;k++) {
    
    
			//抓取每页所有电影的网址
		String[] url=getMovieUrl(k);
//		String item=url[0];
//		System.out.println(getInfos(item)[8]);
		for(int i=0;i<=24;i++)
		{
    
    
			String[] list =getInfos(url[i]);
			//防止豆瓣把你IP禁了
			Thread.sleep(500);
			writeRow(i+1+k*25,list, file);
		}
		}
	}
	//抓取每个电影的链接的函数
	private static String[] getMovieUrl(int j) throws IOException
	{
    
    
		String[] url=new String[25];
		//进入Top250的界面,抓取单个电影的网址
		Document document=Jsoup.connect("https://movie.douban.com/top250?start="+j*25).header("user-agent", "Chrome").get();
        Elements bd=document.select("div.hd");
        for(int i=0;i<bd.size();i++)
        {
    
    
        	Elements info=bd.get(i).select("a[href]");
        	url[i]=info.attr("href");
        }
        return url;
	}
	//获取每个电影的信息,使用的是Jsoup包
	private static String[] getInfos(String url) 
	{
    
    
		//豆瓣电影这个界面十分复杂,情况很多
		try
		{
    
    
		Document document=Jsoup.connect(url).get();
		String info[]=new String[11];
		//抓取电影名
		String name=document.select("span[property=v:itemreviewed]").text();
		info[0]=name;
		//抓取导演名
		String director=document.select("a[rel='v:directedBy']").text();
		info[1]=director;
		//抓取编剧
		int j=1;
		if(document.select("span.attrs").size()>2) {
    
    
		String pl=document.select("span.attrs").get(j).text();
		info[2]=pl;
		//抓取演员名
		String actor=document.select("span.attrs").get(j+1).text();
		info[3]=actor;
		}else if(document.select("span.attrs").size()==2)
		{
    
    
			info[2]=null;
			info[3]=document.select("span.attrs").get(j).text();
		}else
		{
    
    
		    info[2]=null;
		    info[3]=null;
		}
		//抓取电影类型
		String type=document.select("span[property='v:genre']").text();
		info[4]=type;
		//对是否含有官方网站进行讨论
		int i=4;
		if(document.select("span.pl").get(i).text().contains("官方网站"))
		{
    
    
			i++;
		}
		//抓取产地国家,它的内容是国家标签的下一个节点
		String country=document.select("span.pl").get(i).nextSibling().outerHtml();
		info[5]=country;
		//抓取语言
		String lan=document.select("span.pl").get(i+1).nextSibling().outerHtml();
		info[6]=lan;
		//抓取时长
		String runTime=document.select("span[property='v:runtime']").text();
		info[7]=runTime;
		//抓取别名
		String otherName=document.select("span.pl").get(i+4).nextSibling().outerHtml();
		info[8]=otherName;
		//抓取评价人数
		String peoNum=document.select("span[property='v:votes']").text();
		info[9]=peoNum;
		//抓取介绍
		String intro=document.getElementById("link-report").text();
		info[10]=intro;
		return info;
		//出现异常,不要抛出,会导致程序中断
		}catch (IOException e) {
    
    
			// TODO: handle exception
			return null;
		}
	}
	
	//向表格中写数据,使用POI包
	private static void writeRow(int i ,String[] row,File file) throws InvalidFormatException, IOException
	{
    
    
		Workbook workBook=new XSSFWorkbook(new FileInputStream(file));
		//获得Top250Sheet,前提是有这个sheet,没有会报错
		Sheet sheet=workBook.getSheet("Top250");
		Row r=sheet.createRow(i);
		for(int k=0;k<row.length;k++)
		{
    
    
			r.createCell(k).setCellValue(row[k]);
		}
		//将数据写进表格
		workBook.write(new FileOutputStream(file));
		
	}
}

Imagen de efecto

Supongo que te gusta

Origin blog.csdn.net/m0_47202518/article/details/108549784
Recomendado
Clasificación