我试图用Java编写自己的crawljax 3.6插件。它应该告诉crawljax这是一个非常着名的网络爬虫也可以下载文件,他在网页上找到了这些文件。 (PDF,图像等)。我不只想要HTML或实际的DOM-Tree。我想访问他找到的文件(PDF,jpg)。
如何告诉crawljax下载PDF文件,图片等?
感谢您的帮助!
这是我到目前为止 - 使用默认插件(CrawlOverview)的新类:
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import com.crawljax.browser.EmbeddedBrowser.BrowserType;
import com.crawljax.condition.NotXPathCondition;
import com.crawljax.core.CrawlSession;
import com.crawljax.core.CrawljaxRunner;
import com.crawljax.core.configuration.BrowserConfiguration;
import com.crawljax.core.configuration.CrawljaxConfiguration;
import com.crawljax.core.configuration.CrawljaxConfiguration.CrawljaxConfigurationBuilder;
import com.crawljax.core.configuration.Form;
import com.crawljax.core.configuration.InputSpecification;
import com.crawljax.plugins.crawloverview.CrawlOverview;
/**
* Example of running Crawljax with the CrawlOverview plugin on a single-page
* web app. The crawl will produce output using the {@link CrawlOverview}
* plugin.
*/
public final class Main {
private static final long WAIT_TIME_AFTER_EVENT = 200;
private static final long WAIT_TIME_AFTER_RELOAD = 20;
private static final String URL = "http://demo.crawljax.com";
/**
* Run this method to start the crawl.
*
* @throws IOException
* when the output folder cannot be created or emptied.
*/
public static void main(String[] args) throws IOException {
CrawljaxConfigurationBuilder builder = CrawljaxConfiguration
.builderFor(URL);
builder.addPlugin(new CrawlOverview());
builder.crawlRules().insertRandomDataInInputForms(false);
// click these elements
builder.crawlRules().clickDefaultElements();
builder.crawlRules().click("div");
builder.crawlRules().click("a");
builder.setMaximumStates(10);
builder.setMaximumDepth(3);
// Set timeouts
builder.crawlRules().waitAfterReloadUrl(WAIT_TIME_AFTER_RELOAD,
TimeUnit.MILLISECONDS);
builder.crawlRules().waitAfterEvent(WAIT_TIME_AFTER_EVENT,
TimeUnit.MILLISECONDS);
// We want to use two browsers simultaneously.
builder.setBrowserConfig(new BrowserConfiguration(BrowserType.FIREFOX,
1));
CrawljaxRunner crawljax = new CrawljaxRunner(builder.build());
crawljax.call();
}
}
答案 0 :(得分:0)
关于图像 - 我没有看到任何问题,Crawljax加载这些对我来说很好。
关于PDF主题: 不幸的是,Crawljax是硬编码的,可以跳过PDF文件的链接。
请参阅com.crawljax.core.CandidateElementExtractor:342:
/**
* @param href
* the string to check
* @return true if href has the pdf or ps pattern.
*/
private boolean isFileForDownloading(String href) {
final Pattern p = Pattern.compile(".+.pdf|.+.ps|.+.zip|.+.mp3");
Matcher m = p.matcher(href);
if (m.matches()) {
return true;
}
return false;
}
这可以通过修改Crawljax源并为上面的模式引入配置选项来解决。
在Selenium关于非HTML文件的限制适用之后:PDF可以在Firefox JavaScript PDF查看器中查看,出现下载弹出窗口或下载文件。有可能与JavaScript查看器进行交互,无法与下载弹出窗口进行交互,但如果启用了自动下载,则文件将下载到磁盘。
如果您想将Firefox设置为自动下载文件而不弹出下载对话框:
import javax.inject.Provider;
static class MyFirefoxProvider implements Provider<EmbeddedBrowser> {
@Override
public EmbeddedBrowser get() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "/tmp");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream,application/pdf,application/x-gzip");
// disable Firefox's built-in PDF viewer
profile.setPreference("pdfjs.disabled", true);
// disable Adobe Acrobat PDF preview plugin
profile.setPreference("plugin.scan.plid.all", false);
profile.setPreference("plugin.scan.Acrobat", "99.0");
FirefoxDriver driver = new FirefoxDriver(profile);
return WebDriverBackedEmbeddedBrowser.withDriver(driver);
}
}
使用新创建的FirefoxProvider:
BrowserConfiguration bc =
new BrowserConfiguration(BrowserType.FIREFOX, 1, new MyFirefoxProvider());
答案 1 :(得分:0)
使用a[href]
上的CSS选择器getStrippedDom()
手动使用Jsoup获取链接,遍历元素并使用HttpURLConnection
/ HttpsURLConnection
下载它们。