如何在ptyhon中用硒中的html中提取元素的href属性

发布于 2025-01-19 18:28:43 字数 772 浏览 0 评论 0原文

我需要一个图片 URL 列表(之后我将下载它)我不知道如何从类中提取元素并从元素中提取 URL

driver.get("https://pixabay.com/en/photos/search/" + tag +"/?orientation=horizontal&size=medium")
images = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item']/a")))] 
print("Images on Pixabay were found")

        
for x in images:
    images = driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/a[1]")
    images = images.get_attribute("href")
    print(images)
    driver.get(images)
    #this is important because if I did not open a URL(Url after open is change to another.) it did not work
    sec_url = driver.current_url
    print( "Sec URL:  " +sec_url)

I need a make a list of URLs of pictures(after that I will download it ) I do not know how to extract elements from class and from elements extract a URLs

driver.get("https://pixabay.com/en/photos/search/" + tag +"/?orientation=horizontal&size=medium")
images = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item']/a")))] 
print("Images on Pixabay were found")

        
for x in images:
    images = driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/a[1]")
    images = images.get_attribute("href")
    print(images)
    driver.get(images)
    #this is important because if I did not open a URL(Url after open is change to another.) it did not work
    sec_url = driver.current_url
    print( "Sec URL:  " +sec_url)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2025-01-26 18:28:43

图像 是所有 < href> 属性的

images = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item']/a")))] 

列表是否在列表中迭代为:

for x in images:

您需要避免使用相同的变量名称识别元素并存储< href>属性。因此,您需要修改循环的如下:

for x in images:
    element = driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/a[1]")
    element_href = element.get_attribute("href")
    print(element_href)
    driver.get(element_href)
    sec_url = driver.current_url
    print( "Sec URL:  " +sec_url)

images is a list of all the <href> attributes which you have collected using:

images = [my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item']/a")))] 

Moving ahead as you are iterating through the list as:

for x in images:

you need to avoid using the same variable name to identify an element and to store the <href> attribute within the loop. So you need to modify the for loop as follows:

for x in images:
    element = driver.find_element(By.XPATH,"/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/a[1]")
    element_href = element.get_attribute("href")
    print(element_href)
    driver.get(element_href)
    sec_url = driver.current_url
    print( "Sec URL:  " +sec_url)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文