gpt4 book ai didi

java - 使用java从网页中查找href链接

转载 作者:行者123 更新时间:2023-12-01 10:41:23 24 4
gpt4 key购买 nike

我正在读取 URL 并使用 java 中的 URLConnection 获取该页面的响应。

我收到所有 html 内容作为响应。但是当我查看 anchor 标记的 href 链接时,它仅显示

<a href = zzz.html />

但是在浏览器页面中,相应 anchor 标记的原始网址是

<a href = host/zzz.html />

为了获取这个原始网址,我尝试如下,

1. Converting response into string
2. Read each line and check whether that string contains "href="
3. If exists, then I just replace it with "href=url.getHost()"

通过这种方式,我可以获得 URL (--host/zzz.html)。

在某些网站中,页面放置在其他文件夹中并从其他文件夹访问链接。

例如,

我正在查找 (--zzz.com/123/abc/aa.html)。该页面的链接为

<a href = "me.html" /> 

如果我单击此链接,它将在浏览器中转到 (--zzz.com/123/abc/me.html)。在这种情况下,我只能将主机作为 zzz.com。

如果我按照上面提到的 3 个步骤替换 href 链接,链接将更改为 (--zzz.com/me.html)..

如何从 java 代码获取该链接(--zzz.com/123/abc/me.html)。

希望有人能帮忙。

这是我获取 href url 的代码。

    public static void main(String[] argh) throws IOException {
FileWriter fWriter = null;
BufferedWriter writer = null;

URL url = new URL("http://www.nakkheeran.in/Users/frmMagazine.aspx?M=2");
byte[] encodedBytes = Base64.encodeBase64("root:pass".getBytes());
String encoding = new String(encodedBytes);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", "Basic " + encoding);
connection.connect();

InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
try {
fWriter = new FileWriter(new File("f:\\fileName.html"));
writer = new BufferedWriter(fWriter);
while ((line = in.readLine()) != null) {
String s = line.toString();
if (s.contains("<a ")) {
if (s.contains("href=\"http")) {
writer.write(line);
} else if (s.contains("href=\"//")) {
s = s.replace("href=\"//", "href=\"http://");
writer.write(s);
}else if (s.contains("href=\"/")) {
s = s.replace("href=\"/", "href=\"http://" + url.getHost() + "/");
writer.write(s);
} else {
writer.write(s);
}
}
}
writer.close();
}
}

最佳答案

也许你可以尝试使用 jsoup 转到 http://jsoup.org/ 。按照说明进行操作。

(下载 jar 文件并添加到您的类路径中)。

在这里您可以检查该程序。

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HTMLUtils {
private HTMLUtils() {}

public static List<String>extractLinks(String url) throws IOException {
final ArrayList<String> result = new ArrayList<String>();

Document doc = Jsoup.connect("url").get();// enter the url

Elements links = doc.select("a[href]");
Elements media = doc.select("[src]");
Elements imports = doc.select("link[href]");

// href ...To get all the href on that website
for (Element link : links) {
result.add(link.attr("abs:href"));
}

// img ...to get the images from website
for (Element src : media) {
result.add(src.attr("abs:src"));
}

// js, css, ...
for (Element link : imports) {
result.add(link.attr("abs:href"));
}
return result;
}


public final static void main(String[] args) throws Exception{
String site = "url";//enter the url
List<String> links = HTMLUtils.extractLinks(site);
for (String link : links) {
System.out.println(link);
}
}
}

您可以从此程序中获取所有href..

关于java - 使用java从网页中查找href链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34392919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com