【发布时间】:2017-11-07 14:28:00
【问题描述】:
我是 Selenium 的新手,我想从网站下载所有 pdf、ppt(x) 和 doc(x) 文件。我已经编写了以下代码。但我很困惑如何获取内部链接:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebScraper {
String loginPage = "https://blablah/login";
static String userName = "11";
static String password = "11";
static String mainPage = "https://blahblah";
public WebDriver driver = new FirefoxDriver();
ArrayList<String> visitedLinks = new ArrayList<>();
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver.exe");
WebScraper webSrcaper = new WebScraper();
webSrcaper.openTestSite();
webSrcaper.login(userName, password);
webSrcaper.getText(mainPage);
webSrcaper.saveScreenshot();
webSrcaper.closeBrowser();
}
/**
* Open the test website.
*/
public void openTestSite() {
driver.navigate().to(loginPage);
}
/**
* @param username
* @param Password Logins into the website, by entering provided username and password
*/
public void login(String username, String Password) {
WebElement userName_editbox = driver.findElement(By.id("IDToken1"));
WebElement password_editbox = driver.findElement(By.id("IDToken2"));
WebElement submit_button = driver.findElement(By.name("Login.Submit"));
userName_editbox.sendKeys(username);
password_editbox.sendKeys(Password);
submit_button.click();
}
/**
* grabs the status text and saves that into status.txt file
*
* @throws IOException
*/
public void getText(String website) throws IOException {
driver.navigate().to(website);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println("Total no of links Available: " + allLinks.size());
for (int i = 0; i < allLinks.size(); i++) {
String fileAddress = allLinks.get(i).getAttribute("href");
System.out.println(allLinks.get(i).getAttribute("href"));
if (fileAddress.contains("download")) {
driver.get(fileAddress);
} else {
// getText(allLinks.get(i).getAttribute("href"));
}
}
}
/**
* Saves the screenshot
*
* @throws IOException
*/
public void saveScreenshot() throws IOException {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("screenshot.png"));
}
public void closeBrowser() {
driver.close();
}
}
我有一个 if 子句,用于检查当前链接是否为可下载文件(地址包含“下载”一词)。如果是,我会得到它,如果不是,怎么办?那部分是我的问题。我试图实现一个递归函数来检索嵌套链接并重复嵌套链接的步骤,但没有成功。
同时,在输入https://blahblah 时找到的第一个链接是https://blahblah/#,它与https://blahblah 指向同一页面。它也可能导致问题,但目前,我陷入了另一个问题,即递归函数的实现。你能帮帮我吗?
【问题讨论】:
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。请参阅How to Ask 页面以获得澄清此问题的帮助。
-
@DebanjanB 抱歉,我没有问过多个问题。我的问题很简单直接:如何使用 Selenium 遍历网站的所有链接?我不明白您在我的帖子中看到的其他问题。你能解释一下吗?
-
@user1419243 只是尝试删除
else语句,所以只需给for (int i = 0; i < allLinks.size(); i++) { String fileAddress = allLinks.get(i).getAttribute("href"); System.out.println(allLinks.get(i).getAttribute("href")); if (fileAddress.contains("download")) { driver.get(fileAddress); } } -
@DebanjanB 再次抱歉,我认为您还没有阅读我的代码。我已经下载了 pdf 文件,并且在您复制的文本中,我只是在“解释”我的代码。我什么都没问。
-
@user1207289 感谢您的回答。这种方法的问题是它试图在第一页上“仅”下载文件,而不是在内部链接中搜索。而且我实际上在主页中没有任何文件。所以,我需要爬得更深。
标签: java selenium selenium-webdriver web-crawler depth-first-search