我使用 htmlunit 从网页中抓取图像。我是 htmlunit 的初学者。我编码了,但不知道如何获取图像。下面是我的代码。
import java.io.*;
import java.net.URL;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class urlscrap {
public static void main(String[] args) throws Exception
{
//WebClient webClient = new WebClient(Opera);
WebClient webClient = new WebClient();
HtmlPage currentPage = (HtmlPage) webClient.getPage(new URL("http://www.google.com"));
System.out.println(currentPage.asText());
//webClient.closeAllWindows();
}
}
Run Code Online (Sandbox Code Playgroud) 我build.sbt的内容如下:
name := "hello-world"
version := "1.0"
scalaVersion := "2.10.3"
libraryDependencies += "net.sourceforge.htmlunit" %% "htmlunit" % "2.13"
Run Code Online (Sandbox Code Playgroud)
当我update在sbt控制台中执行时,它说:
[error] (*:update) sbt.ResolveException: unresolved dependency: net.sourceforge.htmlunit#htmlunit_2.10;2.13: not found
Run Code Online (Sandbox Code Playgroud)
我该怎么做让sbt找到这个库?
当我使用 HttpUnit 时,我会调用getCurrentPage()HttpUnit 的方法来获取当前页面。我怎样才能在 HtmlUnit 中做到这一点?我试过了webclient.getHomePage(),但它似乎返回了 htmlunit 的网站。
我得到的一个建议是使用 getPage 使用以前的 URL,但这对我不起作用,因为我需要重构之前用代码编写的代码,这使得无法重新执行以前的请求。
所以最近我决定自学如何从网页获取数据。我设法从另一个网页的 JSON 获取数据,但是当我尝试从该网站复制所有内容时,它没有显示我实际需要的数据。
我正在尝试的页面例如: http: //www.tremorgames.com/index.php? action=shop&page=2(您可能需要注册)。我想要获取的数据例如是游戏名称/价格或股票,如果我能得到一个,那么我就能得到全部。
问题是开发工具显示了代码,但是当我尝试使用 Java 将所有内容复制到文件时,它没有显示大部分代码。
(我也尝试过 Jsoup,但它也不起作用)。这是我从网页复制的内容:
BufferedReader reader = null;
try {
URL url = new URL("http://www.tremorgames.com/index.php?action=shop&page=2");
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,我正在努力学习,因此欢迎任何指点(我已经搜索了一段时间,直到我放弃并编写了其余的代码)。
提前致谢。
我要抓取网站,然后使用网站中的数据填充应用程序中的元素,我的网站有登录页面,并且某些页面仅在登录完成后才打开。
我开始使用HtmlUnit,因为它是无头浏览器,并在Java IDE中完成了自定义api,后来我尝试使用从Java IDE生成的jar,发现HtmlUnit和Android存在不兼容问题。
谁能提出解决这个问题的办法?
编辑: 由于没有人真正回答过这个问题,我目前正在使用android的本地WebView进行工作,将其可见性设置为不可见,然后使用与Java对象的javascript接口,我可以注入JS代码来抓取任何数据。
只是玩java试图学习它等等.
到目前为止,这是我的代码,使用HtmlUnit.
package hsspider;
import com.gargoylesoftware.htmlunit.WebClient;
/**
* @author
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("starting ");
Spider spider = new Spider();
spider.Test();
}
}
package hsspider;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
/**
* @author
*/
public class Spider {
public void Test() throws Exception
{
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://www.google.com");
System.out.println(page.getTitleText());
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Netbeans.
我似乎无法弄清楚问题是什么,为什么不编译?
错误:
C:\Users\mrblah\.netbeans\6.8\var\cache\executor-snippets\run.xml:45:
Cancelled by user. …Run Code Online (Sandbox Code Playgroud) 我在测试Grails应用程序的身份验证时遇到了问题.看来浏览器不接受cookie,所以我创建了一个简单的grails应用程序作为测试.
<html>
<head>
<title>Welcome to Grails</title>
</head>
<body>
<g:each in="${request.cookies}">
<h1>${it.name} = <span class="value">${it.value}</span></h1>
</g:each>
<span class="value">test test</span>
</body>
Run Code Online (Sandbox Code Playgroud)
和我的Geb测试:
import spock.lang.Stepwise;
import geb.Page;
import geb.spock.GebReportingSpec
@Stepwise
class LoginSmokeTests extends GebReportingSpec {
String getBaseUrl() {
return "http://localhost:8080/test123/"
}
def "testing stuff"() {
given:
to HomePage
when:
println header
then:
at HomePage
}
}
class HomePage extends Page {
static at = { title == "Welcome to Grails" }
static content = {
header { $("span.value").first().text() }
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过浏览器查看时,会打印2个cookie的值.当通过我的Geb测试访问它时,<span …
根据这个例子,它与它测试的控制器在同一个包中.
为什么这是必需品?
我认为将所有单元测试都放在一个testing包中会比较整洁- 这样做会有问题吗?
package com.example.web.controllers;
...imports...
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/testApplicationContext.xml"})
public class HomeControllerSysTest extends AbstractJUnit4SpringContextTests {
private static final Logger log = Logger.getLogger(
HomeControllerSysTest.class.getName());
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void testHomeController() throws IOException {
final String url = "http://localhost:8080/movie/test";
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage(url);
assertEquals("The Page Title", page.getTitleText());
// there …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 HTMLUnit 登录一个站点,但是每当我提交登录详细信息时,我都会收到大量错误。我把我的代码分成小块,这样我就可以看到它是在点击提交按钮之后,但在其他任何事情发生之前;它需要一段时间,因为它是一个非常慢的网站。不幸的是,因为它发生在登录后,所以我无法向您展示它的含义。我可以说成功登录有一些重定向,因为它给了我一个页面未找到错误,我假设它是导致问题的重定向之一。我以前在 Chrome 上遇到过重定向问题,虽然不是在这个特定的页面上,但现在 Chrome 和 IE8 都在为我加载它。
为您保存完整的堆栈跟踪,以下是最重要的内容:
SEVERE: Error loading JavaScript from [http://servicedeskmt.det.nsw.edu.au:8090/kinetic/displayPage.jsp/../resources/js/jquery/jquery-1.3.2.js].
com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 404 /kinetic/resources/js/jquery/jquery-1.3.2.js for http://servicedeskmt.det.nsw.edu.au:8090/kinetic/resources/js/jquery/jquery-1.3.2.js
at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:535)
INFO: statusCode=[404] contentType=[text/html]
Oct 31, 2011 2:31:29 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO: <html>
<head>
<title>Page cannot be found</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div align="center">
<p> </p>
<p> </p>
<p><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">There was
an error on the page you were attempting to reach or the page could not be
found.</font></b> <br>
</p>
<p><br>
<br> …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Test MVC HtmlUnit和Geb来为我的Spring MVC应用程序驱动功能测试.我想检查在交互过程中是否正确保存了一些会话变量.我尝试创建一个测试控制器来返回这些变量,但HtmlUnit并mvc.perform()使用不同的会话.有没有办法在它们之间使用单个共享会话?
司机设置:
MockMvc mvc = MockMvcBuilders.webAppContextSetup(ctx)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build()
HtmlUnitDriver driver = MockMvcHtmlUnitDriverBuilder.mockMvcSetup(mvc).javascriptEnabled(true).build()
Run Code Online (Sandbox Code Playgroud)
测试:
when:
via ProtectedPage
then:
// this uses session A
at LoginPage
and:
// this uses session B
println mvc.perform(get('/test/sessionAttributes')).andReturn().response.contentAsString
Run Code Online (Sandbox Code Playgroud) 我试图压制HTMLunit在加载页面时几乎总是显示的JavaScript错误.
但奇怪的是,以下代码不起作用:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class HttpClientLogin {
public static void main(String[] args) throws Exception
{
HttpClientLogin logInNow = new HttpClientLogin();
logInNow.loadPage();
}
public void loadPage() throws Exception {
WebClient webClient = new WebClient();
HtmlPage currentPage = webClient.getPage("the url link here");
webClient.setThrowExceptionOnFailingStatusCode(false);
String textSource = currentPage.asText();
String xmlSource = currentPage.asXml();
System.out.println(xmlSource);
}
}
Run Code Online (Sandbox Code Playgroud)
它给出以下错误:
The method setThrowExceptionOnFailingStatusCode(boolean) is undefined for the type WebClient
Run Code Online (Sandbox Code Playgroud)
这些方法是否已被弃用,或者我使用了错误的包?
htmlunit ×11
java ×8
android ×1
geb ×1
groovy ×1
sbt ×1
selenium ×1
spring ×1
spring-mvc ×1
spring-test ×1
unit-testing ×1
web-scraping ×1
webtest ×1