通过将鼠标悬停来抓取Java。抓取后不显示动态数据

时间:2018-09-21 12:37:14

标签: web-scraping jsoup

我想从特定网站的图表中抓取数据。 仅当您将鼠标悬停在图形上时,图形中的此信息才可用。但是,在我抓取后,即使在“检查元素”下可见,我也看不到输出中的数据。

我曾尝试使用JSoup进行抓取,但是当我抓取数据时,不会显示通过鼠标悬停而更改的数据。 我怎样才能做到这一点?

以下是我必须抓取的信息。我必须抓取动态变化的值“ 184”。

The value 184 is dynamically changing when you hover mouse on graph wit h RGB values displyaed in the above line

当您将鼠标悬停在上面一行显示的RGB值的图形上时,值184动态变化。通过将鼠标悬停在图形上,甚至这些RGB值也会改变。

在抓取之后,Jsoup的文档输出如下所示: 数字184和rgb值未出现。这些字段如何在输出中消失?因为鼠标悬停是动态数据,这不会出现吗?

enter image description here

我实际上必须从下图上拖动信息,该图仅将鼠标悬停在“过去24小时内的碳强度”图上即可显示“碳强度”值。

enter image description here

自两天以来,我一直在解决此问题,但没有找到任何有用的解决方案。我在linux上使用Jsoup,有人可以建议我该怎么做。 提前致谢!

1 个答案:

答案 0 :(得分:0)

为此,您应该使用Selenium并将其添加到 Maven (如果正在使用),或添加到正在使用的任何依赖项管理器中。完成此操作后,您需要将此.exe(https://github.com/mozilla/geckodriver/releases)添加到项目文件夹中,以获取对Selenium的 Firefox 支持,您还可以使用 Google Chrome 遵循本教程(https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver)。

您有很多关于如何强制使用网页的JS 来获取其内容的教程,但是将鼠标置于HTML上的某个项目上可能是这样的:

import pyclipper

coordinates = [(198,362),(220,330),(282,372),(260,404)] # Array of lat,lng tuples 
clipper_offset = pyclipper.PyclipperOffset()
coordinates_scaled = pyclipper.scale_to_clipper(coordinates)

clipper_offset.AddPath(coordinates_scaled, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)

new_coordinates = clipper_offset.Execute(pyclipper.scale_to_clipper(10))

new_coordinates_scaled = pyclipper.scale_from_clipper(new_coordinates)

如果您想继续使用 JSOUP 而不是Selenium进行抓取,则可以执行以下操作:

WebDriver webDriver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)webDriver;
webDriver.get(URL); // You have to place the URL you are crawling here

Actions action = new Actions(webDriver);
WebElement webElement = webDriver.findElement(By.id("country-emission-rect));

// using By you have a lot more options to select HTML content, I guess you want to place the mouse over that item in particular, but you can change if it it's another one
action.moveToElement(webElement).perform();

WebDriverWait webDriverWait = new WebDriverWait(webDriver, 15); // wait max 15 seconds

// wait until the element with class name: "country-emission-intensity" is loaded
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("country-emission-intensity")));

// get the HTML generate after the mouse over that now has the text you want to get
String fullHtml = webDriver.getPageSource();
webDriver.quit();

请记住将.exe放置在项目文件夹中并正确安装所有Selenium依赖项(如果使用Maven,则启用自动导入)。

希望它对您有所帮助!如果您还有其他需要,请随时询问!