爬取新股数据测试

This commit is contained in:
dengli
2023-12-19 15:00:41 +08:00
parent 20c4810b4a
commit 3d7546b29e
2 changed files with 16 additions and 21 deletions

View File

@@ -31,6 +31,10 @@ import okhttp3.Request;
import okhttp3.Request.Builder; import okhttp3.Request.Builder;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import cn.stock.market.infrastructure.db.po.QStockPO; import cn.stock.market.infrastructure.db.po.QStockPO;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
@@ -252,27 +256,18 @@ public class StockService {
public ServerResponse getNews() { public ServerResponse getNews() {
String result = ""; String result = "";
try { try {
// 创建URL对象 // 使用Jsoup连接到网页
URL url = new URL("https://www.business-standard.com/markets/news"); Document doc = Jsoup.connect("https://www.business-standard.com/markets/news")
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36")
// 打开URL连接 .header("Referer", "https://www.business-standard.com/")
URLConnection connection = url.openConnection(); .header("Accept-Language", "en-US,en;q=0.9")
.get();
// 获取输入流 // 使用选择器提取所需的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); Elements newsHeadlines = doc.select(".article-listing");
// 遍历提取到的数据并输出
// 读取数据 for (Element headline : newsHeadlines) {
StringBuilder data = new StringBuilder(); System.out.println(headline.text());
String line;
while ((line = reader.readLine()) != null) {
data.append(line);
} }
// 关闭输入流
reader.close();
// 输出抓取到的数据
result = data.toString();
} catch (Exception e) { } catch (Exception e) {
return ServerResponse.createByErrorMsg(e.toString()); return ServerResponse.createByErrorMsg(e.toString());
} }

View File

@@ -209,7 +209,7 @@ public class InvestingInvokerApis {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
JSONObject __market = of().__page(1,100); JSONObject __market = of().__page(1,100);
System.out.println(__market); System.out.println(__market);
JSONObject __page = of().__page(1, 10); JSONObject __page = of().__page(1, 10);
JSONObject __page_nifty100 = of().__page_nifty100(1, 10); JSONObject __page_nifty100 = of().__page_nifty100(1, 10);