如何编写可以浏览网页并在网页上执行操作的自动化机器人

发布于 2024-10-22 11:26:48 字数 309 浏览 6 评论 0原文

我需要编写一个需要执行以下操作的机器人:

转到 jsp 页面并 通过以下方式搜索内容:

  • 1:在搜索框中写一些内容
  • 2:单击搜索按钮(提交按钮)
  • 3:单击结果按钮/链接之一(具有不同输出的相同 jsp 页面)
  • 4:获取该内容的整个 html新页面(具有不同输出的相同 jsp 页面)

第四个页面可以通过屏幕抓取来完成,我认为我不需要帮助。但我需要一些指导来执行从 1 到 3 的选项。任何可以帮助我通过谷歌了解它的链接或只是一些关键字将不胜感激。我打算用java来做这个。

I need to code a bot that needs to do the following:

Go to a jsp page and
search for something by:

  • 1: writing something on a search box
  • 2: clicking the search button(submit button)
  • 3: clicking one of the the resulting buttons/links(same jsp page with different output)
  • 4: get the entire html of the new page(same jsp page with different output)

The 4th one can be done with screen scraping and I do not think I need help with it. But I need some guidance to do the options from 1 to 3. Any links or just some keyword that will help me google to learn about it will be appreciated. I plan to do this with java.

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

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

发布评论

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

评论(6

凉栀 2024-10-29 11:26:48

您所需要的只是 HTMLUnit

这是其描述的摘录

HtmlUnit 是一个“无 GUI 浏览器,用于Java 程序”。它对 HTML 文档进行建模,并提供一个 API,允许您调用页面、填写表单、单击链接等...就像您在“普通”浏览器中所做的那样。

PS:曾用它来构建一个 Web抓取项目;)

All you need is HTMLUnit

This is an extract from its description

HtmlUnit is a "GUI-Less browser for Java programs". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc... just like you do in your "normal" browser.

P.S.: Had used it to build a web scraping project ;)

雅心素梦 2024-10-29 11:26:48

也许这不是你想要的,但你可以尝试 selenium : http://seleniumhq.org/

这是一个 Web 应用程序测试系统。

Maybe it's not what you want, but you can try selenium : http://seleniumhq.org/

It's a web application testing system.

你如我软肋 2024-10-29 11:26:48
const puppeteer = require('puppeteer');

async function searchJSPPage(searchTerm) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  
  // go to the JSP page
  await page.goto('http://example.com/search.jsp');

  // input search term and submit form
  await page.type('input[name="searchBox"]', searchTerm);
  await page.click('input[type="submit"]');
  await page.waitForNavigation();

  // click on one of the resulting links
  await page.click('a[href="/results/1"]'); // example link
  await page.waitForNavigation();

  // get the entire HTML of the new page
  const html = await page.content();
  console.log(html);

  await browser.close();
}

searchJSPPage("example search term");

请注意,这只是一个示例,实际的 JSP 页面可能在输入和按钮上具有不同的属性,而且 JSP 页面可能具有机器人需要处理的一些安全机制,如验证码或 cookie。

const puppeteer = require('puppeteer');

async function searchJSPPage(searchTerm) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  
  // go to the JSP page
  await page.goto('http://example.com/search.jsp');

  // input search term and submit form
  await page.type('input[name="searchBox"]', searchTerm);
  await page.click('input[type="submit"]');
  await page.waitForNavigation();

  // click on one of the resulting links
  await page.click('a[href="/results/1"]'); // example link
  await page.waitForNavigation();

  // get the entire HTML of the new page
  const html = await page.content();
  console.log(html);

  await browser.close();
}

searchJSPPage("example search term");

Please note that this is just an example, the actual JSP page might have different attributes on the input and button, also the JSP page might have some security mechanisms like CAPTCHAs or cookies that the bot need to handle.

三岁铭 2024-10-29 11:26:48

您可以使用 python-mechanize 来实现此目的。

You can use python-mechanize for this.

苏别ゝ 2024-10-29 11:26:48

先决条件:

  1. Selenium API。
  2. Mozilla Firefox(安装了 firebug 扩展)

我们可以通过执行以下操作来实现启动浏览器,转到特定网页,搜索关键字并分析结果启动

  1. 网页浏览器(driver.launch()(selenium)
  2. 转到特定网页( driver.get("your web pager"))(selenium)
  3. 识别搜索框(通过使用 fire bug(id,xml 路径.. 等获取标识符)
  4. 转到该框并写入您的搜索关键字 (webelement.sendkeys("your关键字”)并单击搜索按钮(webelement.click())(selenium)
  5. 使用标识符单击所需的结果并加载下一个网页(selenium)

Prerequistes:

  1. Selenium API.
  2. Mozilla Firefox (with firebug extension installed)

We can achieve launching of a browser,go to the particular web page ,search a keyword and analyse results by doing following

  1. Launch web browser(driver.launch()(selenium)
  2. Go to the particular webpage(driver.get("your web pager"))(selenium)
  3. Identify the search box(get identifier by using fire bug(id,xml path.. etc)
  4. Go to that box and write your search keyword (webelement.sendkeys("your keyword") and click on search button (webelement.click())(selenium)
  5. Click on desired result by using identifiers and for next web page to load (selenium)
携君以终年 2024-10-29 11:26:48

I used selenium in chrome. If you want to use selenium you have to download from http://www.seleniumhq.org/download/ --- the latest version and implement in neatbeans or eclipse the jar files. (Selenium Client & WebDriver Language Bindings, Selenium Standalone Server) After this you have to download from google https://sites.google.com/a/chromium.org/chromedriver/ -- chrome driver also the latest version extract the file and save on your pc.

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