gpt4 book ai didi

java - 使用 Selenium 从任何给定网站查找 Java 中图像的 URL

转载 作者:太空宇宙 更新时间:2023-11-04 11:20:25 26 4
gpt4 key购买 nike

我刚开始使用 Selenium Web Driver,但是我目前正在进行一个项目,该项目需要我仅使用某人的名字从网站上保存该人的图像。我在网上找到的许多解决方案似乎至少在我的案例场景中不起作用。我需要帮助的主要问题是从网页中挑选出特定的图像。例如,如果我要使用链接 https://www.squarespace.com/about/team/我需要能够下载图像,或者至少根据一个人的名字获得一个人的图像的链接。任何信息都会有帮助,谢谢!

最佳答案

这是您问题的答案:

您可以使用以下代码块来检索所有团队成员的姓名以及根据姓名检索一个人的图像的链接:

    import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Q44980374_url_of_an_image
{

public static void main(String[] args)
{

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
WebDriver driver=new ChromeDriver(options);
driver.get("https://www.squarespace.com/about/team/");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,1000)", "");
//Get the Name of the Members
List<WebElement> team_member = driver.findElements(By.xpath("//main[@id='content']//div[@class='team-members']/div[@class='team-member']/div[@class='team-member-text']/h3"));
List<String> mem_name_list = new ArrayList<String>();
for (WebElement member:team_member)
{
String memeber_name = member.getAttribute("innerHTML");
mem_name_list.add(memeber_name);
}
List<WebElement> team_member_images = driver.findElements(By.xpath("//main[@id='content']//div[@class='team-members']/div[@class='team-member']/div[@class='team-member-portrait']/img"));
List<String> mem_image_list = new ArrayList<String>();
for (WebElement image_link:team_member_images)
{
String memebr_image = image_link.getAttribute("src");
mem_image_list.add(memebr_image);
}
for(int i=0;i<team_member.size();i++)
{
System.out.println(mem_name_list.get(i)+"'s image link is : "+mem_image_list.get(i) );
}
}

}

此代码块的输出是:

Anthony Casalena's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/anthony-casalena-300w.jpg
Nicole Anasenes's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/nicole-anasenes-300w.jpg
Andrew Bartholomew's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/andrew-bartholomew-300w.jpg
John Colton's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/john-colton-300w.jpg
Raphael Fontes's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/raphael-fontes-300w.jpg
David Lee's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/david-lee-300w.jpg
Kinjil Mathur's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/kinjil-mathur-300w.jpg
Roberta Meo's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/roberta-meo-300w.jpg
Kris Passet's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/kris-passet-300w.jpg

如果这能回答您的问题,请告诉我。

关于java - 使用 Selenium 从任何给定网站查找 Java 中图像的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44980374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com