志在指尖
用双手敲打未来

java爬虫与python爬虫的区别(全面解读)

java爬虫与python爬虫的区别

jsoup是一款Java的HTML解析器,可直接解析某个URL地址、HTML文本内容。它供给了一套非常省力的API,可经过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。
以博客园首页为例
1、idea新建maven工程
pom.xml导入jsoup依靠
org.jsoup
jsoup
1.12.1
jsoup代码
packagecom.blb;
importorg.jsoup.Jsoup;
importorg.jsoup.nodes.Document;
importorg.jsoup.nodes.Element;
importorg.jsoup.select.Elements;
importjava.io.IOException;
publicclassjsoup{undefined
publicstaticvoidmain(String[]args){undefined
//博客园首页url
finalStringurl=”https://www.cnblogs.com”;
try{undefined
//先获得的是整个页面的html标签页面
Documentdoc=Jsoup.connect(url).get();
System.out.println(doc);
//能够经过元素的标签获取html中的特定元素
Elementstitle=doc.select(“title”);
Stringt=title.text();
System.out.println(t);
//能够经过元素的id获取html中的特定元素
Elementsite_nav_top=doc.getElementById(“site_nav_top”);
Strings=site_nav_top.text();
System.out.println(s);
}catch(IOExceptione){undefined
e.printStackTrace();
}
}
}
该方法有个很大的局限性,便是经过jsoup爬虫只适合爬静态网页,所以只能爬当前页面的信息。
二、Webdriver技术
Selenium是一个浏览器自动化操作结构。selenium主要由三种东西组成。
1.第一个东西——SeleniumIDE,是Firefox的扩展插件,支撑用户录制和回访测试。录制/回访形式存在局限性,对许多用户来说并不适合。
2.因此第二个东西——SeleniumWebDriver供给了各种语言环境的API来支撑更多操控权和编写符合规范软件开发实践的运用程序。
3.最终一个东西——SeleniumGrid协助工程师运用SeleniumAPI操控散布在一系列机器上的浏览器实例,支撑并发运转更多测试。
在项目内部,它们分别被称为“IDE”、“WebDriver”和“Grid”。
什么是Webdriver?
官网介绍:
WebDriverisaclean,fastframeworkforautomatedtestingofwebapps.(WebDriver是一个干净、快速的web运用自动测试结构。)
WebDriver针对各个浏览器而开发,替代了嵌入到被测Web运用中的JavaScript。与浏览器的严密集成支撑创建更高档的测试,避免了JavaScript安全模型导致的限制。除了来自浏览器厂商的支撑,WebDriver还利用操作系统级的调用模仿用户输入。WebDriver支撑Firefox(FirefoxDriver)、IE(InternetExplorerDriver)、Opera(OperaDriver)和Chrome(ChromeDriver)。它还支撑Android(AndroidDriver)和iPhone(IPhoneDriver)的移动运用测试。它还包括一个根据HtmlUnit的无界面完成,称为HtmlUnitDriver。WebDriverAPI能够经过Python、Ruby、Java和C#访问,支撑开发人员运用他们偏心的编程语言来创建测试。
WebDriver如何工作
WebDriver是W3C的一个规范,由Selenium主持。
详细的协议规范能够从http://code.google.com/p/selenium/wiki/JsonWireProtocol#Command_Reference查看。
从这个协议中我们能够看到,WebDriver之所以能够完成与浏览器进行交互,是因为浏览器完成了这些协议。这个协议是运用JOSN经过HTTP进行传输。
它的完成运用了经典的Client-Server形式。客户端发送一个requset,服务器端返回一个response。
我们明确几个概念。
Client
调用WebDriverAPI的机器。
Server
运转浏览器的机器。Firefox浏览器直接完成了WebDriver的通讯协议,而Chrome和IE则是经过ChromeDriver和InternetExplorerDriver完成的。
Session
服务器端需求保护浏览器的Session,从客户端发过来的请求头中包含了Session信息,服务器端将会履行对应的浏览器页面。
WebElement
这是WebDriverAPI中的目标,代表页面上的一个DOM元素。
完成:
2、idea新建maven工程
pom.xml导入入selinium依靠
org.seleniumhq.selenium
selenium-java
3.141.59
代码完成:
packagecom.blb;
importorg.jsoup.Jsoup;
importorg.jsoup.nodes.Document;
importorg.jsoup.select.Elements;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
publicclasschrome{undefined
publicstaticvoidmain(String[]args){undefined
//下载的chromedriver位置
System.setProperty(“webdriver.chrome.driver”,”D:\\idea_workspace\\Jsoup\\src\\main\\chromedriver.exe”);
//实例化ChromeDriver目标
WebDriverdriver=newChromeDriver();
Stringurl=”https://search.51job.com/list/000000,000000,0000,00,9,99,%25E5%25A4%25A7%25E6%2595%25B0%25E6%258D%25AE%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=”;
//翻开指定网站
driver.get(url);
//解析页面
StringpageSource=driver.getPageSource();
Documentjsoup=Jsoup.parse(pageSource);
//界说选择器规则
Stringrule=”#resultList>div:nth-child(4)>p>span>a”;
//经过选择器拿到元素
Elementsselect=jsoup.select(rule);
Strings=select.text();
System.out.println(s);
//模仿浏览器点击
driver.findElement(By.cssSelector(rule)).click();
}
}
爬取电影资源:
packagecom.blb;
importorg.jsoup.Jsoup;
importorg.jsoup.nodes.Document;
importorg.jsoup.nodes.Element;
importorg.jsoup.select.Elements;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
publicclassgetMovie{undefined
privatestaticfinalStringurl=”http://www.zuidazy5.com”;
publicstaticvoidmain(String[]args){undefined
System.setProperty(“webdriver.chrome.driver”,”D:\\idea_workspace\\Jsoup\\src\\main\\chromedriver.exe”);
WebDriverdriver=newChromeDriver();
driver.get(url);
StringpageSource=driver.getPageSource();
Documentjsoup=Jsoup.parse(pageSource);
Stringrule1=”body>div.xing_vb>ul>li>span.xing_vb4>a”;
Elementsselect=jsoup.select(rule1);
//遍历当前页的所有电影概况进口
for(Elemente:select)
{undefined
//获取电影概况页链接
Stringhref=e.attr(“href”);
//进入每个电影概况页面
driver.get(url+href);
StringpageSource2=driver.getPageSource();
Documentjsoup2=Jsoup.parse(pageSource2);
//界说获取电影信息元素的规则
Stringmname=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodh>h2″;
Stringmpic=”body>div.warp>div:nth-child(1)>div>div>div.vodImg>img”;
Stringmdirector=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li:nth-child(2)>span”;
Stringmactor=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li:nth-child(3)>span”;
Stringmarea=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li:nth-child(5)>span”;
Stringmlanguage=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li:nth-child(6)>span”;
Stringmshowtime=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li:nth-child(7)>span”;
Stringmscore=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodh>label”;
Stringmtimelength=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li:nth-child(8)>span”;
Stringmlastmodifytime=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li:nth-child(9)>span”;
Stringminfo=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodinfobox>ul>li.cont>div>span.more”;
Stringmplayaddress1=”#play_1>ul>li”;
Stringmplayaddress2=”#play_2>ul>li”;
Stringmsv=”body>div.warp>div:nth-child(1)>div>div>div.vodInfo>div.vodh>span”;
//获取元素信息
Stringsv=jsoup2.select(msv).text();
Stringname=jsoup2.select(mname).text();
Stringpic=jsoup2.select(mpic).attr(“src”);
Stringdirector=jsoup2.select(mdirector).text();
Stringactor=jsoup2.select(mactor).text();
Stringarea=jsoup2.select(marea).text();
Stringlanguage=jsoup2.select(mlanguage).text();
Stringshowtime=jsoup2.select(mshowtime).text();
Stringscore=jsoup2.select(mscore).text();
Stringtimelength=jsoup2.select(mtimelength).text();
Stringlastmodifytime=jsoup2.select(mlastmodifytime).text();
Stringinfo=jsoup2.select(minfo).text();
Stringplayaddress1=jsoup2.select(mplayaddress1).text();
Stringplayaddress2=jsoup2.select(mplayaddress2).text();
//打印电影名
System.out.println(name);
}
}
}
为了不显现浏览器爬取过程,能够将chromedriver.exe换成无头浏览器phantomjs.exe

java

java爬虫与python爬虫的区别全面解读

java和python在爬虫方面的优势和下风是什么?
Python
强壮的网络功用,模仿登陆,解析Javascript,缺陷是网页解析Python编写程序非常便利,著名的Python爬虫有scratch等
Java
Java有许多解析器,对网页的解析支撑非常好,缺陷是有网络上有许多Java开源爬虫,比方nutch,中国有优异的webmagicjava解析器,比方Htmlparser和jsoup,能够满意Java和python的通用需求。如果需求模仿登陆和反采集,挑选python更便利。如果需求处理杂乱的网页,解析网页内容生成结构化数据或精细解析网页内容,能够挑选Java。
写爬虫用什么言语好?
爬虫挑选什么东西?
1.Crawler是一个网络蜘蛛机器人,它能自动地抓取数据并依据咱们的规矩获取数据
2。为什么运用爬虫?私人定制搜索引擎获取更多数据的年代不再是互联网年代,而是大数据年代
3。爬虫的原理:操控节点(URL分配器)、爬虫节点(依据算法抓取数据并存储在数据库中)、资源库(存储爬虫数据库供给搜索)。爬虫的设计思维:爬虫的网络地址,通过HTTP协议得到相应的HTML页面
5。爬虫言语挑选:
PHP:虽然被评为“世界上最好的言语”,但作为爬虫的缺陷:没有多线程的概念,对异步的支撑很少,并发性不足,爬虫对功率的要求很高
C/CJava:python最大的竞争对手,它非常庞大和粗笨。爬虫需求常常修改代码
Python:言语优美,代码介绍,多方功用模块,调用替代言语接口,以及成熟的分布式战略
Java还能够完结爬虫,如jsoup包,一个非常便利的HTML解析东西。可是,Java言语相对比较麻烦。
java合适做爬虫吗?
Java能够做许多工作,但它是企业应用程序服务端运用最广泛的爬虫程序。当然,以前的公司crawler是由Java完结的
好的,那么这便是易供求网给大家共享的java合适爬虫吗java爬虫与python爬虫的差异,希望大家看完这篇由小编精心整理的内容后,能对相关常识有所了解,处理你的疑问!

未经允许不得转载:IT技术网站 » java爬虫与python爬虫的区别(全面解读)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载