Java学习笔记:爬虫-Selenium高级操作

发布于:2022-12-02 ⋅ 阅读:(781) ⋅ 点赞:(0)

Selenium VS Jsoup

Jsoup

优点:程序的可控性强,不依赖于外部环境,容错性比较强,并发处理更灵活,适合于服务器端开发;

缺点:对于复杂页面(ajax、表单数据校验、登录)处理比较麻烦,对于反爬的网站需要做报文头等的设置工作。

Selenium

优点:简单;

缺点:依赖于外部环境,容错性较差,并发处理比较差,尽量不要再服务器端开发环境中使用;

实现查询单词的几种方式

1、最佳方案:ECDict等非爬虫方案。

2、假如必须用有道词典,怎么做。

3、假如必须用youzack.com,并且用Jsoup。F12查看Json请求。缺点。

4、假如必须用youzack.com,并且用Selenium。Selenium更多操作1

1、模拟按键:

element.sendKeys("你好");

element.sendKeys(Keys.ENTER);

浏览一下WebElement的相关方法,看它能干啥。

2、等待满足条件的元素出现:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“id1")));

如果到期之后找不到,会抛出TimeoutException,需要根据情况进行处理。

浏览一下ExpectedConditions的其他方法。

Selenium更多操作2

尝试查找,找不到就返回null。

findElement如果找不到则会抛异常。

static WebElement tryFindElement(ChromeDriver driver,By by)

{

  var elements = driver.findElements(by);

  if(elements.size()<=0)

  {

  return null;

  }

  else

  {

  return elements.get(0);

  }

}

Selenium查询youzack.com单词

driver.get("https://bdc2.youzack.com/Recitation/Home");

for(String word : words)

{

  WebElement searchInput = driver.findElement(By.id("searchInput"));

  searchInput.clear();

  searchInput.sendKeys(word);

  searchInput.sendKeys(Keys.ENTER);

  WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));

  wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("js_half_screen_dialog")));

  WebElement dialog = driver.findElement(By.id("js_half_screen_dialog"));

  String text = dialog.findElement(By.className("weui-half-screen-dialog__bd")).getText();

  System.out.println(text);

  System.out.println("-----------------");

  dialog.findElement(By.id("dialogClose")).click();

  wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("js_half_screen_dialog")));

}

//求放过

More

1、图形验证码。OCR与打码平台(调用到打码平台人工识别)。

2、行为验证码。突破与打码平台。

3、登录……

4、headless

5、师傅引进门……

6、爬虫更需要具体问题具体分析,别人帮不了你太多。

7、能用正规的接口就不要用爬虫,不仅是技术难度问题,而且有可能有法律问题。

模拟打开网页

https://bdc2.youzack.com/Recitation/Home

然后输入driver

模拟回车

 这样是拿不出来的,我们要等待服务器返回信息,然后在爬取

 

package Part6;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;

public class ChanDanCi {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\PC\\Desktop\\Google\\Chrome\\Application/chromedriver.exe");
        ChromeDriver driver=new ChromeDriver();
        String s="driver";
        driver.get("https://bdc2.youzack.com/Recitation/Home");
        WebElement searchInput = driver.findElement(By.id("searchInput"));
        searchInput.sendKeys(s);
        searchInput.sendKeys(Keys.ENTER);//回车
        WebDriverWait driverWait=new WebDriverWait(driver, Duration.ofSeconds(2));
        driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("weui-half-screen-dialog__bd")));//元素可见的时候在往下走
        WebElement dialog= driver.findElement(By.className("weui-half-screen-dialog__bd"));
        String txt=dialog.getText();
        System.out.println(txt);
        WebElement webElement=tryFindElement(driver,By.id("id1"));
        System.out.println(webElement);
    }
    static WebElement tryFindElement(ChromeDriver driver,By by){
        List<WebElement> elements=driver.findElements(by);
        if (elements.size()>=1){
            return elements.get(0);
        }
        else {
            return null;
        }
    }
}
本文含有隐藏内容,请 开通VIP 后查看