java抓取页面中的keywords
时间: 2023-05-17 18:07:49 浏览: 62
可以使用Java中的Jsoup库来抓取页面中的keywords。具体实现可以参考以下代码:
```
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Main {
public static void main(String[] args) throws Exception {
String url = "https://www.example.com";
Document doc = Jsoup.connect(url).get();
Elements keywords = doc.select("meta[name=keywords]");
for (Element keyword : keywords) {
System.out.println(keyword.attr("content"));
}
}
}
```
这段代码会从指定的网页中抓取所有的keywords,并输出到控制台上。
相关问题
java 抓取网页_java抓取网页
Java 抓取网页的方式有很多种,其中比较常用的方式是使用 Java 提供的 URL 和 HttpURLConnection 类来实现。
以下是一个简单的 Java 抓取网页的示例代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class WebPageFetcher {
public static void main(String[] args) {
try {
// 创建URL对象
URL url = new URL("https://www.example.com");
// 打开URL连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方式为GET
conn.setRequestMethod("GET");
// 获取输入流
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
// 读取数据
while ((line = in.readLine()) != null) {
System.out.println(line);
}
// 关闭输入流
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上代码使用 HttpURLConnection 对象向指定 URL 发送 GET 请求,并将响应数据输出到控制台。可以根据需要进行修改,例如添加请求头、设置超时时间等。
java抓取网页所有文件
在Java中,要抓取网页上的所有文件,可以使用URL类和URLConnection类来实现。以下是一个基本的实现示例:
```java
import java.io.*;
import java.net.*;
public class WebPageDownloader {
public static void main(String[] args) {
String urlString = "http://example.com"; // 替换为目标网页的URL
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
// 设置User-Agent,模拟浏览器请求
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");
// 获取网页的输入流
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
// 逐行读取网页内容
String line;
while ((line = reader.readLine()) != null) {
// 判断是否为文件链接
if (line.contains("<a") && line.contains("href=")) {
int startIndex = line.indexOf("href=\"") + 6;
int endIndex = line.indexOf("\"", startIndex);
String fileUrl = line.substring(startIndex, endIndex);
// 排除非文件链接
if (!fileUrl.contains("#")) {
// 构建文件的URL
URL fileURL = new URL(url, fileUrl);
System.out.println("Downloading file: " + fileURL);
// 下载文件
InputStream fileInputStream = fileURL.openStream();
FileOutputStream fileOutputStream = new FileOutputStream(fileURL.getFile());
byte[] buffer = new byte[1024];
int length;
while ((length = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, length);
}
fileInputStream.close();
fileOutputStream.close();
System.out.println("Download completed!");
}
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请注意,这只是一个简单的示例,并未考虑到所有可能的情况,比如特殊字符的处理等。在实际使用中,可能还需要根据具体需要进行一些调整和改进。