解析url

This commit is contained in:
cyk 2023-12-22 23:36:22 +08:00
parent a9a4cbcf83
commit 165170af97
3 changed files with 94 additions and 0 deletions

11
pom.xml
View File

@ -129,6 +129,8 @@
<artifactId>jackson-annotations</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
@ -147,6 +149,15 @@
<version>3.14</version>
</dependency>
<dependency>
<!-- jsoup HTML parser library @ http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>

View File

@ -16,6 +16,10 @@ import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -24,8 +28,14 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RestController
@RequestMapping("/UrlAndCate")
public class UrlAndCateController {
@ -279,4 +289,75 @@ public class UrlAndCateController {
return JSON.toJSONString(result);
}
}
@RequestMapping ("/disposeUrl")
public String disposeUrl(String url){
HashMap<String, Object> result = new HashMap<>();
HashMap<String, Object> data = new HashMap<>();
Connection conn = Jsoup.connect(url);
Document document = null;
try {
document = conn.get();
} catch (IOException e) {
e.printStackTrace();
result.put("code", 500);
result.put("msg", "url不可用");
// handle error
}
result.put("code", 200);
result.put("msg", "解析成功");
//添加标题
data.put("title",document.title());
Elements linksContent = document.select("meta[name='keywords']");
Elements linksDescription = document.select("meta[name='description']");
Elements linksIcon = document.select("link[rel$=icon]");
String fengli="content=\"(.*?)\"";
Pattern pafengli=Pattern.compile(fengli);
Matcher description = pafengli.matcher(linksDescription.toString());
String target;
if(description.find())
{
target = description.group(1); //group为捕获组
data.put("description",target);
}
else
{
data.put("description","未找到");
}
description = pafengli.matcher(linksContent.toString());
if(description.find())
{
target = description.group(1); //group为捕获组
data.put("keywords",target);
}
else
{
data.put("keywords","未找到");
}
String fengli1="href=\"(.*?)\"";
Pattern pafengli1=Pattern.compile(fengli1);
description = pafengli1.matcher(linksIcon.toString());
if(description.find())
{
target = description.group(1); //group为捕获组
try {
new URL(target).toURI();
data.put("iconUrl",target);
} catch (MalformedURLException e) {
data.put("iconUrl","url不合法");
} catch (URISyntaxException e) {
data.put("iconUrl","url不合法");
}
}
else
{
data.put("iconUrl","未找到");
}
result.put("data",data);
return JSONObject.toJSONString(result);
}
}

View File

@ -1,11 +1,13 @@
package com.lovenav.dao;
import com.lovenav.entity.Nav;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface NavDao {
int deleteByNavName(String navName);