2017-10-06 62 views
-2

我试图从网页https://iaeme.com/ijmet/index.asp下载所有pdf文件。导航到for循环中的下一个页面而不会失败循环?

页面有不同的链接,每个链接里面有多个下载和更多的页面。我正在尝试导航下一页并继续循环。

package flow; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.StandardCopyOption; 
import java.util.List; 
import java.util.NoSuchElementException; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.apache.tools.ant.taskdefs.Java; 
import org.apache.tools.ant.types.FileList.FileName; 
import org.openqa.selenium.By; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebDriver.Navigation; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.w3c.dom.Text; 

import jxl.common.Assert; 
//kindly ignore the imports 


public class excel { 

    public static void main(String[] args) throws IOException, Exception { 

     System.setProperty("webdriver.chrome.driver", "C:\\Users\\User_2\\Downloads\\chromedriver_win32\\chromedriver.exe"); 
     WebDriver d=new ChromeDriver(); 
     d.manage().window().maximize(); 
     d.get("https://iaeme.com/ijmet/index.asp");     
     java.util.List<WebElement> catvalues=d.findElements(By.className("issue")); 
     for(int i=0;i<=catvalues.size();i++){ 
      catvalues.get(i).click();      
      java.util.List<WebElement> downcount=d.findElements(By.linkText("Download")); 
      System.out.println(downcount.size()); 

      for(int k=1;k<=downcount.size();k++){ 
       downcount.get(k).click();             
       Thread.sleep(5000);       
      } 

      d.navigate().back(); 
      catvalues = d.findElements(By.className("issue")); 
     } 
    } 
} 

我试过了不同的方法失败了。

回答

1

如果检查https://iaeme.com/ijmet/index.asp页面,你可以注意到,与ID 每个类LIK存在的onclick属性。在此属性中,您需要打开所有感兴趣页面的信息。

例子:

模式是

onclick="journalpissue('8','9','IJMET')" 

并从此你必须

https://iaeme.com/ijmet/issues.asp?JType=IJMET&VType=8&IType=9

因此,创建这个环节,在这个例子:

由Vtype = 8 ITYPE = 9 =因为Jtype IJMET

一旦你把所有的链接,你可以遍历所有页面。

对于每个页面,您必须获得所有类别为id为jounl的元素的“href”属性值。

一旦你有了pdf链接,我继续使用“curl”命令。如果你想下载所有与硒的文件,可能是有用的这个答案https://stackoverflow.com/a/37664671/3881320

public class Stackoverflow { 

public static void main(String args[]) { 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("https://iaeme.com/ijmet/index.asp"); 
     java.util.List<WebElement> likValues = driver.findElements(By.className("lik")); 
     LinkedList<String> allUrl = new LinkedList<>(); 
     String baseUrl = "https://iaeme.com/ijmet/"; 
     for (WebElement el : likValues) { 
      String journalpissue = el.getAttribute("onclick"); 
      String relativeUrl = parseJournalpissue(journalpissue); 
      allUrl.add(relativeUrl); 
     } 

     for (String url : allUrl) { 
      analyzePage(driver, baseUrl + url, true); 
     } 

    } 

private static void analyzePage(WebDriver driver, String url, boolean searchOtherPages) { 
     driver.get(url); 
     List<WebElement> allA = null; 
     if (searchOtherPages) { 
      List<WebElement> tdlist = driver.findElements(By.cssSelector("table[class='contant'] tr td")); 
      WebElement pages = tdlist.get(tdlist.size() - 1); 
      System.out.println(pages.getText()); 
      allA = pages.findElements(By.tagName("a")); 
     } 

     java.util.List<WebElement> jounl = driver.findElements(By.className("jounl")); 
     for (WebElement wel : jounl) { 
      String href = wel.getAttribute("href"); 
      if (href.contains(".pdf")) { 
       System.out.println("File to download: " + href); 
       downloadFile(href); 
      } 
     } 

     if (allA != null) { 
      for (WebElement a : allA) { 
       String href = a.getAttribute("href"); 
       System.out.println(href); 
       analyzePage(driver, href, false); 
      } 
     } 
    } 


private static void downloadFile(String file) { 
     try { 
      String[] CMD_COMPOSED = { 
       "/bin/bash", 
       "-c", 
       "curl -O " + file,}; 
      String output; 

      Process p = Runtime.getRuntime().exec(CMD_COMPOSED); 
      StringBuilder outputBuilder; 
      outputBuilder = new StringBuilder(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8)); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
       outputBuilder.append(line + "\n"); 
      } 
      output = outputBuilder.toString(); 
     } catch (IOException ex) { 
      Logger.getLogger(Stackoverflow.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    private static String parseJournalpissue(String journalpissue) { 
     String finalUrl = null; 

     StringTokenizer st = new StringTokenizer(journalpissue, "'"); 
     st.nextToken(); 
     String vType = st.nextToken(); 
     st.nextToken(); 
     String iType = st.nextToken(); 
     st.nextToken(); 
     String jType = st.nextToken(); 

     finalUrl = "issues.asp?JType=" + jType + "&VType=" + vType + "&IType=" + iType; 
     System.out.println(finalUrl); 
     return finalUrl; 

    } 
} 

注意:我并不认为在这些页面(那里有PDF文件下载)有可能会有更多页面(您的“更多页面”,在您的描述中)。为了做到这一点,你可以使用相同的方法。

编辑:

有关页数的相关信息是:

enter image description here

即在表类名 “含量的不同”。特别是最后一个因素。

所以:

List<WebElement> tdlist = driver.findElements(By.cssSelector("table[class='contant'] tr td")); 
WebElement pages = tdlist.get(tdlist.size() - 1); 

我们有兴趣有关 “一个” 变量名:

List<WebElement> allA = pages.findElements(By.tagName("a")); 

现在我们又有了所有其他网页的URL。我们可以使用以前的相同方法来下载pdf文件。

+0

谢谢哥们,我会试试这个。 – SarathChandar

+0

好友,我可以下载,但是,我无法导航到下一页,你能帮我吗? – SarathChandar

+0

我编辑了答案并插入了允许您在其他页面上导航的部分。我确信有其他(最佳)方法可以获得最终目标。我希望我帮助你理解如何思考如何在这类问题中寻找解决方案。 –