简体   繁体   English

使用硒下载图像

[英]Download images using selenium

I want to download and save images using selenium in python2.7我想在python2.7中使用selenium下载和保存图像

I've tried:我试过了:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

url= "https://in.images.search.yahoo.com/images/view;_ylt=A2oKiHPRis1VplIALaEO9olQ;_ylu=X3oDMTIyN2I2OHZkBHNlYwNzcgRzbGsDaW1nBG9pZANjN2U1ZjU4NjAwMDQ1MDA0OGExZGMxY2Y0MzMyMDk0MwRncG9zAzEEaXQDYmluZw--?.origin=&back=https%3A%2F%2Fin.images.search.yahoo.com%2Fyhs%2Fsearch%3Fp%3D%2522Eiffel%2BGreens%2522%2BBalewadi%2509Pune%26n%3D60%26ei%3DUTF-8%26y%3DSearch%26type%3Dff.40.w81.hp.04-01.in.avg._.0715av%26fr%3Dyhs-avg-fh_lsonsw%26fr2%3Dsb-top-in.images.search.yahoo.com%26hsimp%3Dyhs-fh_lsonsw%26hspart%3Davg%26tab%3Dorganic%26ri%3D1&w=556&h=309&imgurl=www.propertyonepune.com%2Fimg%2Fgallery%2F0becda3e53f8db646a699e54b1333a4c.jpg&rurl=http%3A%2F%2Fwww.propertyonepune.com%2Fproperties%2F46%2FBalewadi&size=49.8KB&name=...+bungalows+by+Eiffel+Developers+%26+Realtors+Ltd.+at+%3Cb%3EBalewadi%3C%2Fb%3E%2C+%3Cb%3EPune%3C%2Fb%3E&p=%22Eiffel+Greens%22+Balewadi%09Pune&oid=c7e5f586000450048a1dc1cf43320943&fr2=sb-top-in.images.search.yahoo.com&fr=yhs-avg-fh_lsonsw&tt=...+bungalows+by+Eiffel+Developers+%26+Realtors+Ltd.+at+%3Cb%3EBalewadi%3C%2Fb%3E%2C+%3Cb%3EPune%3C%2Fb%3E&b=0&ni=21&no=1&ts=&tab=organic&sigr=11lu74lc1&sigb=17t67hvmu&sigi=1284god0v&sigt=12i2gtekb&sign=12i2gtekb&.crumb=wZ3uTmSmDfL&fr=yhs-avg-fh_lsonsw&fr2=sb-top-in.images.search.yahoo.com&hsimp=yhs-fh_lsonsw&hspart=avg&type=ff.40.w81.hp.04-01.in.avg._.0715av"
driver = webdriver.Firefox()
driver.get(url)

path = '//div[@class="iholder"]//img[@src]'
for k in driver.find_elements_by_xpath(path):
    items = []
    src = (k.get_attribute('src')).encode('utf8')
    items.append(src)
    print items
    for lm in items:
        driver.get(lm)
        driver.sendKeys(Keys.Control + "s")
        driver.send_keys(Keys.Enter)

It's giving me error:它给了我错误:

Traceback (most recent call last):
File "C:/Users/Heypillow/Desktop/download.py", line 17, in <module>
driver.sendKeys(Keys.Control + "s")
AttributeError: 'WebDriver' object has no attribute 'sendKeys'

I've tried with:我试过:

driver.send_keys(Keys.CONTROL + "s")

Same error is showing显示相同的错误
What should I do to save the images?我应该怎么做才能保存图像? Thanks in advance提前致谢

It looks like you want to save the html for each picture, so you could use actions to get the context-menu of firefox -> "p" is shortcut for save page:看起来您想为每张图片保存 html,因此您可以使用操作来获取 firefox 的上下文菜单 -> "p" 是保存页面的快捷方式:

for lm in items:
        driver.get(lm)
        body = driver.find_element(By.tagName("body"));
        ActionChains(driver).move_to_element(body).context_click(htmlElement).send_keys("p").send_keys(Keys.RETURN).perform();

I'm usually using Java, so there might be some typos in this python code of mine ;-)我通常使用 Java,所以我的这个 python 代码中可能有一些拼写错误;-)

Actually, op's first attempt is more correct than the selected answer.实际上,op 的第一次尝试比选择的答案更正确。 If you're not sending keys to an element for typing then you're sending them to the browser for shortcuts, etc.如果您没有将键发送到元素以进行输入,那么您将它们发送到浏览器以获取快捷方式等。

ActionChains(driver).key_down(Keys.Control).send_keys("s").key_up(Keys.Control)‌​‌​.perform()

The error you are getting is because .send_keys does not hang off of webdriver , it hangs off of webelement .你得到的错误是因为.send_keys没有挂在webdriver ,它挂在webelement You need to get a webelement first before trying to use .send_keys .在尝试使用.send_keys之前,您需要先获取一个webelement For example,例如,

for lm in items:
    lm.sendKeys(Keys.Control + "s")

This isn't going to answer your main question but it does explain why you are getting the error message.这不会回答您的主要问题,但它确实解释了您收到错误消息的原因。

To answer your main question, google it and you will find many responses such as this one that already has answers.要回答你的主要问题,谷歌它,你会发现很多反应,如这一个已经有答案。

The Accepted Answer has had one change since 2015.自 2015 年以来,已接受的答案发生了一个变化。

Instead of代替

Keys.Control

It has now changed to现在已经改为

Keys.CONTROL

and the snippet changes to并且片段更改为

ActionChains(browser).key_down(Keys.CONTROL).send_keys("s").key_up(Keys.CONTROL).perform()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM