网站建设资讯

NEWS

网站建设资讯

java图片爬取代码 Java爬取图片

java爬虫读取某一张指定图片的url,求解答

使用jsoup解析到这个url就行,dom结构如下:

创新互联建站专注于舟曲网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供舟曲营销型网站建设,舟曲网站制作、舟曲网页设计、舟曲网站官网定制、小程序制作服务,打造舟曲网络公司原创品牌,更为您提供舟曲网站排名全网营销落地服务。

look-inside-cover类只有一个,所以直接找到这个img元素,获取src属性,就可以获取到图片路径。

代码实现如下:

Document doc = Jsoup.connect("").get();

Elements imgs = doc.select(".look-inside-cover");

String imgUrl = imgs.get(0).attr("src");

jsoup的jar包分享给你:

怎么用java从文件中读取图片和写入图片到文件里

首先导入各种需要的包:

import java.awt.Image;

import javax.imageio.ImageIO;

import java.io.*;

读取图片的方法如下:

Image[] array = new Image[10];

Image image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧

array[0] = image;

图片读出来了。

如果你有一个Image对象,想把它写入文件可以这样做:

BufferedImage image = ImageIO.read(new File("d:\\source.gif"));

//要想保存这个对象的话你要把image声明为BufferedImage 类型

ImageIO.write(image, "png", new File("f:\\test.png"));

java如何读取文件夹中的图片并在界面显示

下面给你提供一个实现,该实现采用了代理模式。这个实现包含两个文件,分别是Client.java和ImageIcoProxy.java,ImageIcoProxy.java负责了图片的延迟加载,你可以修改为不延迟即可。

Client.java的代码为:

import java.awt.Graphics;

import java.awt.Insets;

import javax.swing.Icon;

import javax.swing.JFrame;

public class Client extends JFrame {

private static int IMG_WIDTH = 510;

private static int IMG_HEIGHT = 317;

private Icon imgProxy = null;

public static void main(String[] args) {

Client app = new Client();

app.setVisible(true);

}

public Client() {

super("Virture Proxy Client");

imgProxy = new ImageIcoProxy("D:/test.jpg", IMG_WIDTH, IMG_HEIGHT);

this.setBounds(100, 100, IMG_WIDTH + 10, IMG_HEIGHT + 30);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void paint(Graphics g) {

super.paint(g);

Insets insets = getInsets();

imgProxy.paintIcon(this, g, insets.left, insets.top);

}

}

ImageIcoProxy.java的代码为:

import java.awt.Component;

import java.awt.Graphics;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.SwingUtilities;

public class ImageIcoProxy implements Icon {

private ImageIcon realIcon = null;

private String imgName;

private int width;

private int height;

boolean isIconCreated = false;

public ImageIcoProxy(String imgName, int width, int height) {

this.imgName = imgName;

this.width = width;

this.height = height;

}

public int getIconHeight() {

return realIcon.getIconHeight();

}

public int getIconWidth() {

return realIcon.getIconWidth();

}

public void paintIcon(final Component c, Graphics g, int x, int y) {

if (isIconCreated) {

//已经加载了图片,直接显示

realIcon.paintIcon(c, g, x, y);

g.drawString("Just Test", x + 20, y + 370);

} else {

g.drawRect(x, y, width-1, height-1);

g.drawString("Loading photo...", x+20, y+20);

synchronized(this) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

try {

Thread.currentThread().sleep(2000);

realIcon = new ImageIcon(imgName);

isIconCreated = true;

} catch (Exception e) {

e.printStackTrace();

}

c.repaint();

}

}

);

}

}

}

}

Java中怎么抓取网页中的图片

通过httpclient来爬取网站内容,分析当前内容页中的图片‘规则’

抓取一般都是模拟浏览器访问目标网页,通过返回的页面html代码进行分析自己需要的数据

查找规则,例如你爬取的网页 ,看到当前页面显示的图片格式如下img src=""

通过解析爬取的网页源代码(html)进行字符串的操作即可,现在有相应的第三方jar包可以帮你更快的完成这部分工作,例如htmlpaser,获取到对应的地址,然后进行保存或下载。

你可以搜索,java爬虫(httpclient)和htmlpaser做更多的了解。

用java怎么读取图片

思路:使用 java.awt.Image包下的Image可以接收图片。读取则使用ImageIO对象。

代码如下:

/**

* 读取图片,首先导入以下的包

*/

import java.awt.Image;

import javax.imageio.ImageIO;

import java.io.*;

/**

* 用Image对象来接收图片

* 路径根据实际情况修改

*/

Image image = ImageIO.read(new File("c:\\1.png"));

System.out.println(image.getSource());

如何用java代码获取 这个网页上的图片

你也没说是要把图片下载下来还是什么的

这里我实现的是将图片下载到电脑

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

public class DownLoad {

public static void main(String[] args) {

File dstFile = new File("E:/test/test/test.jpg");

try {

URL url = new URL("");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

InputStream is = conn.getInputStream();

saveFile(is, dstFile);

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void saveFile(InputStream is,File dstFile){

FileOutputStream fos = null;

File parentFile = dstFile.getParentFile();

if(!parentFile.exists()){

parentFile.mkdirs();

}

try {

fos = new FileOutputStream(dstFile);

byte[] buff = new byte[1024 * 4];

int len;

while((len = is.read(buff)) != -1){

fos.write(buff, 0, len);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

try{

if(is != null){

is.close();

}

if(fos != null){

fos.close();

}

}catch(IOException e){

e.printStackTrace();

}

}

}

}


网站题目:java图片爬取代码 Java爬取图片
网页URL:http://cdweb.net/article/docecpi.html