本文主要是介绍万能爬虫selenium,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
环境准备:
根据浏览器版本,下载与之对应的浏览器驱动包(这里以google浏览器为例)
http://npm.taobao.org/mirrors/chromedriver
大版本号相同即可
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zy</groupId><artifactId>epidemic-crawling</artifactId><version>0.0.1-SNAPSHOT</version><name>epidemic-crawling</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><druid.version>1.2.6</druid.version><spring-boot.mybatis>2.1.4</spring-boot.mybatis><fastjson.version>1.2.76</fastjson.version><common-pool.version>2.6.2</common-pool.version><mybatis-plus.version>3.2.0</mybatis-plus.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-context</artifactId><version>2.0.1.RELEASE</version></dependency><!-- Mysql Connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--引入druid数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><!-- Mybatis 依赖配置 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${spring-boot.mybatis}</version></dependency><!-- JSON 解析器和生成器 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency><!-- 公共资源池 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>${common-pool.version}</version></dependency><!-- mybatis-plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><!-- 对象转换工具类 --><dependency><groupId>ma.glasnost.orika</groupId><artifactId>orika-core</artifactId><version>1.5.1</version></dependency><!-- hutool --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.1</version></dependency><!-- pagehelper 分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.3.0</version></dependency><!-- 工具类库 --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.15</version></dependency><!-- jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version></dependency><!-- 文件下载 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency><!-- selenium-java --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins><resources><resource><directory>lib</directory><targetPath>/BOOT-INF/lib/</targetPath><includes><include>**/*.jar</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.yml</include><include>**/mapper/**</include></includes></resource></resources></build></project>
public static void main(String[] args) {System.setProperty("webdriver.chrome.driver","D:\\a\\chromedriver.exe");WebDriver webDriver = new ChromeDriver();webDriver.get("https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner&city=%E5%B1%B1%E4%B8%9C-%E5%B1%B1%E4%B8%9C");/* List<WebElement> elements = webDriver.findElements(By.className("VirusTable_1-1-319_2AH4U9"));for (int i = 0; i < elements.size(); i++) {List<WebElement> tdElements = elements.get(i).findElements(By.tagName("td"));String template = "地区:{0},新增:{1},现有:{2},累计:{3},治愈:{4},死亡{5}";String resultTemp= MessageFormat.format(template, new Object[] {tdElements.get(0).getText(),tdElements.get(1).getText(),tdElements.get(2).getText(),tdElements.get(3).getText(),tdElements.get(4).getText(),tdElements.get(5).getText()});System.out.println(resultTemp);}*/List<WebElement> elements = webDriver.findElements(By.className("ProvinceSummary_1-1-319_Tdrvui"));for (int i = 0; i < elements.size(); i++) {List<WebElement> tdElements = elements.get(i).findElements(By.tagName("p"));String template = "{0}:{1}";String resultTemp= MessageFormat.format(template, new Object[] {tdElements.get(0).getText(),tdElements.get(1).getText()});System.out.println(resultTemp);}driver.close();}
这篇关于万能爬虫selenium的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!