0
点赞
收藏
分享

微信扫一扫

用Java爬虫程序来采集美图录图片

Villagers 2023-12-18 阅读 48

用Java爬虫程序来采集美图录图片_java

这个Java爬虫程序使用了Jsoup库来解析HTML页面,并使用HttpURLConnection来发送HTTP请求和接收HTTP响应。

```java
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Crawler {q:181545036
    public static void main(String[] args) {
        try {
            // 创建URL对象
            URL url = new URL("http://www.meitupu.com/");

            // 创建HttpURLConnection对象
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            // 设置代理服务器
            httpURLConnection.setProxyHost("www.duoip.cn");
            httpURLConnection.setProxyPort(8000);

            // 设置请求方法
            httpURLConnection.setRequestMethod("GET");

            // 设置请求头
            httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

            // 连接服务器
            httpURLConnection.connect();

            // 获取响应码
            int responseCode = httpURLConnection.getResponseCode();
            System.out.println("响应码: " + responseCode);

            // 创建BufferedReader对象
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

            // 创建StringBuilder对象
            StringBuilder stringBuilder = new StringBuilder();

            // 读取服务器响应数据
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }

            // 关闭BufferedReader对象
            bufferedReader.close();

            // 使用jsoup解析服务器响应数据
            Document document = Jsoup.parse(sb.toString());

            // 获取所有的img标签
            Elements imgElements = document.select("img");

            // 遍历获取的img标签
            for (Element element : imgElements) {
                // 获取img标签的src属性值
                String src = element.attr("src");

                // 创建URL对象
                URL url2 = new URL(src);

                // 创建HttpURLConnection对象
                HttpURLConnection httpURLConnection2 = (HttpURLConnection) url2.openConnection();

                // 设置请求方法
                httpURLConnection2.setRequestMethod("GET");

                // 设置请求头
                httpURLConnection2.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");

                // 连接服务器
                httpURLConnection2.connect();

                // 获取响应码
                int responseCode2 = httpURLConnection2.getResponseCode();
                System.out.println("响应码: " + responseCode2);

                // 创建BufferedReader对象
                BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(httpURLConnection2.getInputStream()));

                // 创建StringBuilder对象
                StringBuilder stringBuilder2 = new StringBuilder();

                // 读取服务器响应数据
                String line2;
                while ((line2 = bufferedReader2.readLine()) != null) {
                    stringBuilder2.append(line2);
                }

                // 关闭BufferedReader对象
                bufferedReader2.close();

                // 获取图片内容
                String picContent = stringBuilder2.toString();

                // 获取图片名称
                String picName = src.substring(src.lastIndexOf("/") + 1);

                // 保存图片到本地
                File file = new File("F:/meitupu/" + picName);
                FileOutputStream out = new FileOutputStream(file);
                out.write(picContent.getBytes());
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

程序首先创建了一个URL对象,然后使用URL对象创建了一个HttpURLConnection对象。接着,程序设置了代理服务器,并设置了请求方法和请求头。然后,程序连接服务器,并获取了响应码。如果响应码为200,表示请求成功,程序接下来就会读取服务器的响应数据,并使用Jsoup解析响应数据。然后,程序获取了所有的img标签,遍历获取的img标签,获取了img标签的src属性值,并创建了一个新的URL对象。然后,程序使用新的URL对象创建了一个新的HttpURLConnection对象,设置了请求方法和请求头,连接服务器,获取了响应码,并读取了服务器的响应数据。然后,程序获取了图片内容,并获取了图片名称,最后保存图片到本地。

举报

相关推荐

用JS键盘事件来移动图片

0 条评论