加载异步脚本后 Java 抓取网站

发布于 2025-01-09 01:35:34 字数 874 浏览 4 评论 0原文

背景很少,我试图为客户提供直接添加 HTML 并发布单页网站(如 blogspot)的选项。这带来了诈骗者的问题,所以我创建了一个微服务来阻止基于 HTML 内容的发布网站。

最初我使用 JSoup 从网站获取 HTML,现在骗子已经变异并使用外部网站加载脚本,并且它是异步加载的

所以我最初渲染的 HTML 没有任何诈骗内容,因此它逃避了网站拦截。我试图在脚本完全加载后或在某个固定时间后抓取网站内容。

我尝试过,但总是得到预黑客脚本加载的 HTML。

Document doc = Jsoup.connect("http://example.com")
  .data("query", "Java")
  .userAgent("Mozilla")
  .cookie("auth", "token")
  .timeout(3000)
  .post();

并尝试了 htmlunit

        WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setCssEnabled(false);
        HtmlPage page = webClient.getPage("http://example.com");

在所有脚本都用 Java 加载后,有没有一种优雅的方式来抓取网站?

Little background, I'm trying to given an option for customer to add HTML directly and publish a single page website(like blogspot). This brought scammers problem, so I created a microservice that blocks publishing website based on HTML content.

Initially I used JSoup for getting HTML from website, now the scammer has mutated and is using an external website for loading script and it is loaded in async
<script src="https://yolologroyopuedo.us/?api=1&lan=fbcacaroto" type="text/javascript" async="true"></script>

So my initial rendered HTML does not have any scam content so it evades the website blocking. I'm trying to scrape website content after the script has loaded completely or after some fixed time.

I tried but I'm always getting pre hacking script loaded HTML.

Document doc = Jsoup.connect("http://example.com")
  .data("query", "Java")
  .userAgent("Mozilla")
  .cookie("auth", "token")
  .timeout(3000)
  .post();

and tried htmlunit

        WebClient webClient = new WebClient();
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setCssEnabled(false);
        HtmlPage page = webClient.getPage("http://example.com");

is there an elegant way to scrape a website after all scripts are loaded in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2025-01-16 01:35:34

您正在讨论的脚本是在浏览器中执行的 - 如果您想在脚本之后获取页面,

  • 则不能使用 jsoup,因为 jsoup 根本没有 js 支持,因此无法
  • 使用 HtmlUnit 处理脚本,您必须启用js 支持,然后可能等待脚本的执行(例如 webclient.waitForBackgroundJavaScript())。之后页面中的 dom 树就会更新,您可以使用常用的选择器来获取您想知道的内容。

如果您仍然遇到问题,请在 github 上打开 HtmlUnit 问题,并包含您所使用的 url,以便我们有机会重现您的案例。

The script you are talking about is executed in you browser - if you like to get the page after the script

  • you can't use jsoup because jsoup has no js support at all and therefore can't process the script
  • with HtmlUnit you have to enable js support and then maybe wait for the execution (e.g. webclient.waitForBackgroundJavaScript()) of the script. After that the dom tree in the page is updated and you can use the usual selectors to get what you like to know.

If you still have problems please open an HtmlUnit issue on github and include the url you ear working with to give us a chance to reproduce your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文