图片-小白之家
图片-小白之家
图片-小白之家
图片-小白之家

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爬虫与python爬虫的区别全面解读,

© 版权声明
THE END
喜欢就支持一下吧
点赞0分享
评论 抢沙发

请登录后发表评论

    暂无评论内容

图片-小白之家
图片-小白之家