问题
I'm currently wondering how to deal with fragment identities, a link that I am wanting to grab information from, contains a fragment identity. It seems as if HtmlUnit is discarding the "#/db4mj" of my url and therefore loading the original url.
Does anyone know of a way to deal with fragment identities? (I can post example code to further explain if need be)
EDIT
Since I wasn't getting many views (and no answers), I'm going to add a bounty. Sorry it's only 50, but I only had 79 to start with
EDIT
Here is an example code as requested.
Our URL will be: http://browse.deviantart.com/resources/applications/psbrushes/?order=9&offset=0
So if you take a look at the content in the link, you'll see multiple brushes that contain URLs as well. So my script grabs the URL: http://browse.deviantart.com/resources/applications/psbrushes/?order=9&offset=0#/dbwam4
As you can see there is the fragment identifier #/dbwam4 Now I try and grab the content that is on this page, but HtmlUnit still thinks it is on the original URL.
Here is an the example code in my script where it fails on the fragment identifier URL but has no problem with the original URL.
client = new WebClient(BrowserVersion.FIREFOX_3)
client.javaScriptEnabled = false
page = client.getPage(url) //url with fragment identifier
//this is on the url with the fragment identifier only, not the original url
img = page.getByXPath("*[@id="gmi-ResViewSizer_img"]")
I'm expecting to be able to grab certain information from the URL with the fragment identifier but am unable to access it whatsoever.
回答1:
There is good news and bad news.
First the good news is that HtmlUnit appears to be working just fine.
If you visit the page with the fragment identier URL in a browser with JavaScript turned off (maybe using Firefox's QuickJava plugin), you will not see the "single brush view" that you want.
So in order to acquire this page you need to use WebClient with setJavaScriptEnabled set to true.
And now the bad news:
I have not consistently been able to acquire the "single brush view" page using HtmlUnit with JavaScript turned on (I know not why). Although, I have been able to acquire the full page on occassion.
The real problem is the state of the returned HTML is so bad as to defy my attempts to parse it (I tried TagSoup, jsoup, Jaxen, etc). I therefore suspect attempting to parse the page using XPath may not work for you.
I would therefore think you need to resort to using regular expressions (which is far from ideal) or even use some variant of String.indexOf("gmi-ResViewSizer_img").
I hope this helps.
EDIT
I managed to get something that sporadically works. I'm afraid I am not converted to Groovy yet, so it will be in plain old Java.
I haven't looked at the source of HtmlUnit but it is almost as if something in the process of running the save is helping to make the parsing work?? Without the save I seem to get NullPointerExceptions.
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.FalsifyingWebConnection;
import java.io.File;
import java.io.IOException;
public class TestProblem {
public static void main(String[] args) throws IOException {
WebClient client = new WebClient(BrowserVersion.FIREFOX_3_6);
client.setJavaScriptEnabled(true);
client.setCssEnabled(false);
String url = "http://browse.deviantart.com/resources/applications/psbrushes/?order=9&offset=0#/dbwam4";
client.setThrowExceptionOnScriptError(false);
client.setThrowExceptionOnFailingStatusCode(false);
client.setWebConnection(new FalsifyingWebConnection(client) {
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
if ("www.google-analytics.com".equals(request.getUrl().getHost())) {
return createWebResponse(request, "", "application/javascript"); // -> empty script
}
if ("d.unanimis.co.uk".equals(request.getUrl().getHost())) {
return createWebResponse(request, "", "application/javascript"); // -> empty script
}
if ("edge.quantserve.com".equals(request.getUrl().getHost())) {
return createWebResponse(request, "", "application/javascript"); // -> empty script
}
if ("b.scorecardresearch.com".equals(request.getUrl().getHost())) {
return createWebResponse(request, "", "application/javascript"); // -> empty script
}
//
if (request.getUrl().toString().startsWith("http://st.deviantart.net/css/v6core_jc.js")) {
WebResponse wr = super.getResponse(request);
return createWebResponse(request, wr.getContentAsString(), "application/javascript");
}
if (request.getUrl().toString().startsWith("http://st.deviantart.net/css/v6loggedin_jc.js")) {
WebResponse wr = super.getResponse(request);
return createWebResponse(request, wr.getContentAsString(), "application/javascript");
}
return super.getResponse(request);
}
});
HtmlPage page = client.getPage(url); //url with fragment identifier
File saveFile = new File("saved.html");
if(saveFile.exists()){
saveFile.delete();
saveFile = new File("saved.html");
}
page.save(saveFile);
HtmlElement img = page.getElementById("gmi-ResViewSizer_img");
System.out.println(img.toString());
}
}
来源:https://stackoverflow.com/questions/4588199/htmlunit-and-fragment-identities