使用 Java 进行网页抓取
我找不到任何好的基于 Java 的 Web 抓取 API。我需要抓取的网站也没有提供任何API;我想使用一些 pageID
迭代所有网页,并提取 DOM 树中的 HTML 标题/其他内容。
除了网页抓取之外还有其他方法吗?
I'm not able to find any good web scraping Java based API. The site which I need to scrape does not provide any API as well; I want to iterate over all web pages using some pageID
and extract the HTML titles / other stuff in their DOM trees.
Are there ways other than web scraping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
jsoup
提取标题并不困难,并且您有很多选择,请在 Stack Overflow 上搜索“Java HTML 解析器”。其中之一是 Jsoup。
如果您了解页面结构,则可以使用 DOM 导航页面,请参阅
http://jsoup.org/cookbook/extracting-data/dom-navigation
这是一个很好的库,我在最近的项目中使用了它。
jsoup
Extracting the title is not difficult, and you have many options, search here on Stack Overflow for "Java HTML parsers". One of them is Jsoup.
You can navigate the page using DOM if you know the page structure, see
http://jsoup.org/cookbook/extracting-data/dom-navigation
It's a good library and I've used it in my last projects.
最好的选择是使用 Selenium Web Driver,因为它
为编码人员提供视觉反馈(查看正在执行的抓取操作,查看停止的位置)
准确且一致,因为它直接控制您使用的浏览器。
慢。不像 HtmlUnit 那样点击网页,但有时你不想点击太快。
Htmlunit 速度很快,但在处理 Javascript 和 AJAX 方面却很糟糕。
Your best bet is to use Selenium Web Driver since it
Provides visual feedback to the coder (see your scraping in action, see where it stops)
Accurate and Consistent as it directly controls the browser you use.
Slow. Doesn't hit web pages like HtmlUnit does but sometimes you don't want to hit too fast.
Htmlunit is fast but is horrible at handling Javascript and AJAX.
HTMLUnit可以用来做网页抓取,它支持调用页面、填充和抓取。提交表格。我已经在我的项目中使用了这个。这是一个很好的用于网页抓取的java库。
阅读此处了解更多
HTMLUnit can be used to do web scraping, it supports invoking pages, filling & submitting forms. I have used this in my project. It is good java library for web scraping.
read here for more
Java 的 mechanize 非常适合这种情况,正如 Wadjy Essam 提到的,它使用 JSoup 来实现 HMLT。 mechanize 是一个阶段式 HTTP/HTML 客户端,支持导航、表单提交和页面抓取。
http://gistlabs.com/software/mechanize-for-java/(和GitHub 此处 https://github.com/GistLabs/mechanize)
mechanize for Java would be a good fit for this, and as Wadjy Essam mentioned it uses JSoup for the HMLT. mechanize is a stageful HTTP/HTML client that supports navigation, form submissions, and page scraping.
http://gistlabs.com/software/mechanize-for-java/ (and the GitHub here https://github.com/GistLabs/mechanize)
还有 Jaunt Java Web Scraping & JSON 查询 - http://jaunt-api.com
There is also Jaunt Java Web Scraping & JSON Querying - http://jaunt-api.com
您可以查看 jwht-scraper!
这是一个完整的抓取框架,具有开发人员期望从网络抓取中获得的所有功能:
它与 (jwht-htmltopojo)[https://github.com/whimtrip/jwht-htmltopojo) lib 配合使用使用其他几个人提到的 Jsoup 这里。
它们将帮助您构建出色的抓取工具,将 HTML 直接映射到 POJO,并在几分钟内绕过任何经典的抓取问题!
希望这可以帮助这里的一些人!
免责声明,我是它的开发者,请随时让我知道您的言论!
You might look into jwht-scraper!
This is a complete scraping framework that has all the features a developper could expect from a web scraper :
It works with (jwht-htmltopojo)[https://github.com/whimtrip/jwht-htmltopojo) lib which itsef uses Jsoup mentionned by several other people here.
Together they will help you built awesome scrapers mapping directly HTML to POJOs and bypassing any classical scraping problems in only a matter of minutes!
Hope this might help some people here!
Disclaimer, I am the one who developed it, feel free to let me know your remarks!
查看 HTML 解析器,例如 TagSoup、HTMLCleaner 或 NekoHTML。
Look at an HTML parser such as TagSoup, HTMLCleaner or NekoHTML.
如果您希望自动抓取大量页面或数据,那么您可以尝试 Gotz ETL。
它像真正的 ETL 工具一样完全由模型驱动。数据结构、任务工作流和要抓取的页面是使用一组 XML 定义文件定义的,无需编码。可以使用带有 JSoup 的选择器或带有 HtmlUnit 的 XPath 来编写查询。
If you wish to automate scraping of large amount pages or data, then you could try Gotz ETL.
It is completely model driven like a real ETL tool. Data structure, task workflow and pages to scrape are defined with a set of XML definition files and no coding is required. Query can be written either using Selectors with JSoup or XPath with HtmlUnit.
对于此类任务,我通常使用 Crawler4j + Jsoup。
使用crawler4j,我从域下载页面,您可以使用正则表达式指定哪个ULR。
我用jsoup“解析”了你用crawler4j搜索并下载的html数据。
通常你也可以使用 jsoup 下载数据,但 Crawler4J 可以更轻松地查找链接。
使用crawler4j的另一个优点是它是多线程的,您可以配置并发线程数
https://github .com/yasserg/crawler4j/wiki
For tasks of this type I usually use Crawller4j + Jsoup.
With crawler4j I download the pages from a domain, you can specify which ULR with a regular expression.
With jsoup, I "parsed" the html data you have searched for and downloaded with crawler4j.
Normally you can also download data with jsoup, but Crawler4J makes it easier to find links.
Another advantage of using crawler4j is that it is multithreaded and you can configure the number of concurrent threads
https://github.com/yasserg/crawler4j/wiki
通常我使用selenium,这是一个用于测试自动化的软件。
您可以通过网络驱动程序控制浏览器,因此您不会遇到 JavaScript 问题,并且如果您使用完整版本,通常不会被检测到。无头浏览器可以更容易被识别。
Normally I use selenium, which is software for testing automation.
You can control a browser through a webdriver, so you will not have problems with javascripts and it is usually not very detected if you use the full version. Headless browsers can be more identified.