feat(service): 重命名并重构抖音爬虫服务类
将原来的 Test.java 重命名为 FtbCrawlNetDy.java,并将其重构为 Spring 组件。 实现了 FtbCrawlNetBase 接口,用于拦截抖音登录后的 Cookie。 移除了 main 方法和 Scanner 手动登录逻辑,改为通过 PlatformSelectionView 触发执行。更新了页面导航地址为抖音登录页,并保留浏览器上下文监听 Cookie 请求的功能。 删除了原有的 PlaywrightInterceptCookieExample 示例类,相关功能已整合进新结构。
This commit is contained in:
@@ -1,68 +0,0 @@
|
|||||||
package com.fantaibao;
|
|
||||||
|
|
||||||
import com.microsoft.playwright.*;
|
|
||||||
import com.microsoft.playwright.options.Cookie;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
|
|
||||||
public class PlaywrightInterceptCookieExample {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
try (Playwright playwright = Playwright.create()) {
|
|
||||||
// 启动可见浏览器
|
|
||||||
Browser browser = playwright.chromium().launch(
|
|
||||||
new BrowserType.LaunchOptions().setHeadless(false)
|
|
||||||
);
|
|
||||||
|
|
||||||
// 创建浏览器上下文
|
|
||||||
BrowserContext context = browser.newContext();
|
|
||||||
|
|
||||||
// 用于存储拦截到的Cookie
|
|
||||||
ConcurrentMap<String, String> interceptedCookies = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
// 监听网络请求
|
|
||||||
context.onRequest(request -> {
|
|
||||||
// 打印请求URL
|
|
||||||
System.out.println("请求URL: " + request.url());
|
|
||||||
|
|
||||||
// 获取请求头中的Cookie
|
|
||||||
String cookieHeader = request.headerValue("cookie");
|
|
||||||
if (cookieHeader != null && !cookieHeader.isEmpty()) {
|
|
||||||
System.out.println("请求中的Cookie: " + cookieHeader);
|
|
||||||
interceptedCookies.put(request.url(), cookieHeader);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Page page = context.newPage();
|
|
||||||
|
|
||||||
// 导航到示例页面(这里使用百度作为示例)
|
|
||||||
page.navigate("https://www.baidu.com");
|
|
||||||
|
|
||||||
System.out.println("页面加载完成,请执行点击操作...");
|
|
||||||
System.out.println("点击按钮后按回车键继续获取Cookie...");
|
|
||||||
|
|
||||||
// 等待用户执行点击操作
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
scanner.nextLine();
|
|
||||||
|
|
||||||
// 获取当前页面的所有Cookie
|
|
||||||
List<Cookie> cookies = context.cookies();
|
|
||||||
System.out.println("\n当前页面的Cookie:");
|
|
||||||
for (Cookie cookie : cookies) {
|
|
||||||
System.out.println(cookie.name + "=" + cookie.value +
|
|
||||||
" (域名: " + cookie.domain + ", 路径: " + cookie.path + ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 显示拦截到的请求Cookie
|
|
||||||
System.out.println("\n拦截到的请求Cookie:");
|
|
||||||
interceptedCookies.forEach((url, cookie) ->
|
|
||||||
System.out.println("URL: " + url + "\nCookie: " + cookie + "\n"));
|
|
||||||
|
|
||||||
// 保持浏览器打开一段时间以便查看
|
|
||||||
System.out.println("浏览器将在10秒后关闭...");
|
|
||||||
page.waitForTimeout(10000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
package com.fantaibao;
|
|
||||||
|
|
||||||
import com.microsoft.playwright.*;
|
|
||||||
import com.microsoft.playwright.options.Cookie;
|
|
||||||
import com.microsoft.playwright.options.WaitForSelectorState;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Test {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// 启动可见浏览器
|
|
||||||
try (Playwright playwright = Playwright.create();
|
|
||||||
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
|
|
||||||
|
|
||||||
BrowserContext context = browser.newContext();
|
|
||||||
// 监听网络请求
|
|
||||||
context.onRequest(request -> {
|
|
||||||
// 打印请求URL
|
|
||||||
System.out.println("请求URL: " + request.url());
|
|
||||||
// 获取请求头中的Cookie
|
|
||||||
String cookieHeader = request.headerValue("cookie");
|
|
||||||
if (cookieHeader != null && !cookieHeader.isEmpty()) {
|
|
||||||
System.out.println("请求中的Cookie: " + cookieHeader);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Page page = context.newPage();
|
|
||||||
|
|
||||||
// 导航到登录页面
|
|
||||||
page.navigate("https://ecom.meituan.com/bizaccount/login.html?loginByPhoneNumber=true&isProduction=true&epassportParams=%3Fbg_source%3D1%26service%3Dcom.sankuai.meishi.fe.ecom%26part_type%3D0%26feconfig%3Dbssoify%26biz_line%3D1%26continue%3Dhttps%253A%252F%252Fecom.meituan.com%252Fbizaccount%252Fbiz-choice.html%253Fredirect_uri%253Dhttps%25253A%25252F%25252Fecom.meituan.com%25252Fmeishi%25252F%2526_t%253D1759399140148%2526target%253Dhttps%25253A%25252F%25252Fecom.meituan.com%25252Fmeishi%25252F%26leftBottomLink%3D%26signUpTarget%3Dself");
|
|
||||||
// page.navigate("https://playwright.dev", new Page.NavigateOptions().setTimeout(6000000));
|
|
||||||
|
|
||||||
// 等待用户手动登录
|
|
||||||
System.out.println("请在浏览器中手动登录...");
|
|
||||||
System.out.println("登录完成后按回车键继续...");
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
scanner.nextLine();
|
|
||||||
|
|
||||||
// 等待元素可见并启用后再点击
|
|
||||||
page.locator(".getStarted_Sjon").waitFor(new Locator.WaitForOptions().setState(WaitForSelectorState.VISIBLE));
|
|
||||||
page.locator(".getStarted_Sjon").click();
|
|
||||||
|
|
||||||
|
|
||||||
// 获取登录后的Cookie
|
|
||||||
List<Cookie> cookies = context.cookies();
|
|
||||||
for (Cookie cookie : cookies) {
|
|
||||||
System.out.println(cookie.name + "=" + cookie.value);
|
|
||||||
}
|
|
||||||
// 保持浏览器打开一段时间以便查看
|
|
||||||
System.out.println("浏览器将在10秒后关闭...");
|
|
||||||
page.waitForTimeout(10000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -118,6 +118,12 @@ public class PlatformSelectionView {
|
|||||||
connectButton.setCursor(javafx.scene.Cursor.HAND);
|
connectButton.setCursor(javafx.scene.Cursor.HAND);
|
||||||
connectButton.setStyle("-fx-background-color: " + buttonColor + "; -fx-text-fill: white; -fx-padding: 10px 20px; -fx-border-radius: 4px;");
|
connectButton.setStyle("-fx-background-color: " + buttonColor + "; -fx-text-fill: white; -fx-padding: 10px 20px; -fx-border-radius: 4px;");
|
||||||
|
|
||||||
|
connectButton.setOnAction(event -> {
|
||||||
|
FtbCrawlNetBase ftbCrawlNetMt = SpringContext.getBean("ftbCrawlNetDy");
|
||||||
|
ftbCrawlNetMt.executeCookieIntercept();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
option.getChildren().addAll(imageView, titleLabel, connectButton);
|
option.getChildren().addAll(imageView, titleLabel, connectButton);
|
||||||
|
|
||||||
return option;
|
return option;
|
||||||
|
|||||||
52
src/main/java/com/fantaibao/service/FtbCrawlNetDy.java
Normal file
52
src/main/java/com/fantaibao/service/FtbCrawlNetDy.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package com.fantaibao.service;
|
||||||
|
|
||||||
|
import com.fantaibao.base.FtbCrawlNetBase;
|
||||||
|
import com.microsoft.playwright.*;
|
||||||
|
import com.microsoft.playwright.options.Cookie;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
@Component(value = "ftbCrawlNetDy")
|
||||||
|
public class FtbCrawlNetDy implements FtbCrawlNetBase {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private Playwright playwright;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeCookieIntercept() {
|
||||||
|
// 启动可见浏览器
|
||||||
|
try (Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false))) {
|
||||||
|
|
||||||
|
BrowserContext context = browser.newContext();
|
||||||
|
// 监听网络请求
|
||||||
|
context.onRequest(request -> {
|
||||||
|
// 打印请求URL
|
||||||
|
System.out.println("请求URL: " + request.url());
|
||||||
|
// 获取请求头中的Cookie
|
||||||
|
String cookieHeader = request.headerValue("cookie");
|
||||||
|
if (cookieHeader != null && !cookieHeader.isEmpty()) {
|
||||||
|
System.out.println("请求中的Cookie: " + cookieHeader);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Page page = context.newPage();
|
||||||
|
|
||||||
|
// 导航到登录页面
|
||||||
|
page.navigate("https://life.douyin.com/p/login?channel_id=baidupc&key_word=842434586104&bd_vid=10266644552663920872",
|
||||||
|
new Page.NavigateOptions().setTimeout(6000000));
|
||||||
|
|
||||||
|
// 获取登录后的Cookie
|
||||||
|
List<Cookie> cookies = context.cookies();
|
||||||
|
for (Cookie cookie : cookies) {
|
||||||
|
System.out.println(cookie.name + "=" + cookie.value);
|
||||||
|
}
|
||||||
|
// 保持浏览器打开一段时间以便查看
|
||||||
|
System.out.println("浏览器将在10秒后关闭...");
|
||||||
|
page.waitForTimeout(10000);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user