如何在ptyhon中用硒中的html中提取元素的href属性
我需要一个图片 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
图像 是所有
< href>
属性的列表是否在列表中迭代为:
您需要避免使用相同的变量名称识别元素并存储
< href>
属性。因此,您需要修改循环的如下:
images is a list of all the
<href>
attributes which you have collected using:Moving ahead as you are iterating through the list as:
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 thefor
loop as follows: