长春网络营销网站,太原市建设工程交易中心网站,海天建设集团网站,浙江省城乡与住房建设厅网站目录
一、jsoup的使用
1.概述
2.主要功能
3.快速入门
4.数据准备
二、Selenium
1.概述
2.使用
三、Selenium配合jsoup获取数据
四、爬虫准则
五、Seleniumjsoupmybatis实现数据保存
1.筛选需要的数据
2.创建一个表#xff0c;准备存储数据 手写#xff1f;不存在…目录
一、jsoup的使用
1.概述
2.主要功能
3.快速入门
4.数据准备
二、Selenium
1.概述
2.使用
三、Selenium配合jsoup获取数据
四、爬虫准则
五、Seleniumjsoupmybatis实现数据保存
1.筛选需要的数据
2.创建一个表准备存储数据 手写不存在的
一、jsoup的使用
1.概述
jsoup 是一款 Java 的 HTML 解析器可直接解析某个 URL 地址、HTML 文本内容。它提供了一套非常省力的 API可通过 DOMCSS 以及类似于 jQuery 的操作方法来取出和操作数据。
2.主要功能 从URL文件或字符串中刮取并解析HTML 查找和提取数据使用DOM遍历或CSS选择器 操纵HTML元素属性和文本 根据安全的白名单清理用户提交的内容以防止XSS攻击 输出整洁的HTML
3.快速入门 引入jsoup坐标 然后就可以直接开整了
dependencygroupIdorg.jsoup/groupIdartifactIdjsoup/artifactIdversion1.17.2/version
/dependency
Java
public static void main(String[] args) throws IOException {Document doc Jsoup.connect(https://www.baidu.com).get();String title doc.title();System.out.println(Title is: title);
}
参数设置
Document doc Jsoup.connect(http://example.com).data(query, Java).userAgent(Mozilla).cookie(auth, token).timeout(3000).post();
结束使用就是这么快 使用需要的知识 Java基础 html三剑客基础【知道是干啥的就行】html/css/javascript jQuery基础 4.数据准备
我这里主要是为了获取数据更深入的知识可自行网上查阅我就不写了
Element类方法详解 - jsoup - 文档中心 - 技术客 (sunjs.com) 首先准备一个网址我用的jd的
笔记本 - 商品搜索 - 京东热卖 (jd.com) 回到Java程序运行一次看看有数据不 String s 笔记本;final String encode URLEncoder.encode(s);
Document doc null;try {doc Jsoup.connect(https://re.jd.com/search?keywordencode).userAgent(Mozilla).timeout(5000).get();} catch (IOException e) {e.printStackTrace();}System.out.println(doc);
输出如下图即可 打开网页f12或鼠标右键点击检查选择要获取数据容器/元素的位置 之后分析html代码根据类名/标签名/id等缩小范围即可 回到Java代码选择一下标签输出看一下
final Element shopList doc.getElementById(shop_list);
System.out.println(shopList); 发现是个空数据由于jsoup只能爬取静态页面所以需要借助另外一个工具了
拓展
数据并不是不存在它只是在js里 如果不嫌麻烦的话可以使用data方法获取到但是后续处理很麻烦就不写了 二、Selenium
这是测试方向的但别说真好用这个我没咋学过只会一点有时间研究研究~
1.概述
Selenium是一系列基于Web的自动化工具提供一套测试函数用于支持Web自动化测试。函数非常灵活能够完成界面元素定位、窗口跳转、结果比较。
具有如下特点 多浏览器支持 如IE、Firefox、Safari、Chrome、Android手机浏览器等。 支持多语言 如Java、C#、Python、Ruby、PHP等。 支持多操作系统 如Windows、Linux、IOS、Android等。 开源免费 官网Selenium
2.使用 引入Selenium依赖坐标 根据不同浏览器下载不同的驱动有些可能需要点魔法 设置浏览器驱动 我的电脑–属性–系统设置–高级–环境变量–系统变量–Path将“存放浏览器驱动”目录添加到Path的值中。 验证浏览器是否能启动成功 !-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --
dependencygroupIdorg.seleniumhq.selenium/groupIdartifactIdselenium-java/artifactIdversion4.0.0/version
/dependency
验证浏览器 public static void main(String[] args) {
// WebDriver driver new ChromeDriver(); //Chrome浏览器
// WebDriver driver new FirefoxDriver(); //Firefox浏览器WebDriver driver new EdgeDriver(); //Edge浏览器
// WebDriver driver new InternetExplorerDriver(); // Internet Explorer浏览器
// WebDriver driver new OperaDriver(); //Opera浏览器
// WebDriver driver new PhantomJSDriver(); //PhantomJSSystem.out.println(driver);}
如果报错还会给最新的驱动下载地址这很nice 驱动版本一致后运行 开始挨个解决异常信息
首先第一个访问给的网址
SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See SLF4J Error Codes for further details.
翻译一下 添加依赖 dependencygroupIdch.qos.logback/groupIdartifactIdlogback-classic/artifactIdversion1.2.11/version/dependency
运行第一个问题解决 第二个问题 jdk11的有另外一种解决方案我这用的jdk8的11的可自行搜一下 EdgeOptions edgeOptions new EdgeOptions();edgeOptions.addArguments(--remote-allow-origins*);WebDriver driver new EdgeDriver(edgeOptions);
之后所有问题解决开始使用~ 测试代码 EdgeOptions edgeOptions new EdgeOptions();edgeOptions.addArguments(--remote-allow-origins*);WebDriver driver new EdgeDriver(edgeOptions);// 2.打开百度首页driver.get(https://www.baidu.com);// 3.获取输入框输入seleniumdriver.findElement(By.id(kw)).sendKeys(selenium);// 4.获取“百度一下”按钮进行搜索driver.findElement(By.id(su)).click();// 5.退出浏览器//driver.quit();
运行完美到这一步后后面就非常简单了~ 三、Selenium配合jsoup获取数据
java代码
Scanner scanner new Scanner(System.in);
System.out.println(请输入要搜索的内容);
final String s 笔记本;
final String encode URLEncoder.encode(s);
EdgeOptions edgeOptions new EdgeOptions();
edgeOptions.addArguments(--remote-allow-origins*);
//创建浏览器窗口
WebDriver edgeDriver new EdgeDriver(edgeOptions);
edgeDriver.get(https://re.jd.com/search?keywordencode);
//动态网站数据填充比较慢需要延迟才可以拿到数据一般网不差3到5s就差不多了
Thread.sleep(5000);
//获取页面数据
final String pageSource edgeDriver.getPageSource();
//将字符串转为document对象
final Document parse Jsoup.parse(pageSource);
final Element shopList parse.getElementById(shop_list);
//检查一下是否获得数据了
System.out.println(shopList.html());
数据有了~ 推荐先直接把获取到的html数据持久化到本地~爬的次数多了会被监视的~ 四、爬虫准则
爬虫访问频次要控制别把对方服务器搞崩溃了涉及到个人隐私的信息不要也不能爬突破网站的反爬措施后果很严重如果你被监视了最好不要尝试突破反爬措施遵守robot.txt----(Robots协议) 五、Seleniumjsoupmybatis实现数据保存
1.筛选需要的数据 Scanner scanner new Scanner(System.in);System.out.println(请输入要搜索的内容);final String s 笔记本电脑;final String encode URLEncoder.encode(s);EdgeOptions edgeOptions new EdgeOptions();edgeOptions.addArguments(--remote-allow-origins*);//创建浏览器窗口WebDriver edgeDriver new EdgeDriver(edgeOptions);edgeDriver.get(https://re.jd.com/search?keywordencode);//动态网站数据填充比较慢需要延迟才可以拿到数据一般网不差3到5s就差不多了Thread.sleep(5000);//获取页面数据final String pageSource edgeDriver.getPageSource();//将字符串转为document对象final Document parse Jsoup.parse(pageSource);final Element shopList parse.getElementById(shop_list);final Elements li shopList.children();System.out.println(----------------------------------------------------------------------------);// //div[classsear_container w]/div[2]/div/div/div/ul/li/*li.html()还有俩部分一个是pic一个是li_cen_botli_cen_bot再分3个commodity_info商品信息价格;commodity_tit:商品标题;comment:评论*/ArrayListString images new ArrayList();ArrayListString prices new ArrayList();ArrayListString titles new ArrayList();ArrayListString comments new ArrayList();//.get(1).attr(src).substring(2))li.forEach(inner - {images.add(https: inner.getElementsByClass(img_k).attr(src));titles.add(inner.getElementsByClass(commodity_tit).text());if (inner.getElementsByClass(price).text().length()0){prices.add(预约);}else{prices.add(inner.getElementsByClass(price).text().substring(1));}comments.add(inner.getElementsByClass(praise praise-l).text());}); 2.创建一个表准备存储数据
先随便整一个吧~ 加个img_url字段字段长度如果太小了就自己再改改有错误自己再调调有基础这些错误都能自己调了
3.结果
mybatis配置啥的不写了直接出结果代码了
public class DemoJ {public static void main(String[] args) throws InterruptedException, IOException {Scanner scanner new Scanner(System.in);System.out.println(请输入要搜索的内容);final String s 笔记本电脑;final String encode URLEncoder.encode(s);
EdgeOptions edgeOptions new EdgeOptions();edgeOptions.addArguments(--remote-allow-origins*);//创建浏览器窗口WebDriver edgeDriver new EdgeDriver(edgeOptions);
edgeDriver.get(https://re.jd.com/search?keywordencode);//动态网站数据填充比较慢需要延迟才可以拿到数据一般网不差3到5s就差不多了Thread.sleep(5000);//获取页面数据final String pageSource edgeDriver.getPageSource();
//将字符串转为document对象final Document parse Jsoup.parse(pageSource);final Element shopList parse.getElementById(shop_list);
final Elements li shopList.children();System.out.println(----------------------------------------------------------------------------);
// //div[classsear_container w]/div[2]/div/div/div/ul/li/*li.html()还有俩部分一个是pic一个是li_cen_botli_cen_bot再分3个commodity_info商品信息价格;commodity_tit:商品标题;comment:评论*/ArrayListString images new ArrayList();ArrayListString prices new ArrayList();ArrayListString titles new ArrayList();ArrayListString comments new ArrayList();//.get(1).attr(src).substring(2))li.forEach(inner - {images.add(https: inner.getElementsByClass(img_k).attr(src));titles.add(inner.getElementsByClass(commodity_tit).text());if (inner.getElementsByClass(price).text().length()0){prices.add(预约);}else{prices.add(inner.getElementsByClass(price).text().substring(1));}comments.add(inner.getElementsByClass(praise praise-l).text());});
String resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);
final SqlSession sqlSession sqlSessionFactory.openSession();final ProductMapper mapper sqlSession.getMapper(ProductMapper.class);
for (int i 0; i images.size(); i) {mapper.add(titles.get(i),prices.get(i),images.get(i),comments.get(i));}sqlSession.commit();sqlSession.close();}
} 结束