'with ...as..' 下载图像硒

问题描述

嗨,我通过自动下载谷歌图片,将 selenium 与 python 结合使用

使用'with ...as'下载图片,因为某些网站阻止了抓取请求, 所以我必须在下载时添加 cookie 和 header can't download image with python

但是当我在里面使用'for image in images'时它只下载了0字节的图像

我认为这是获取 url 部分的问题,但事实并非如此

    count = 1
    images = driver.find_elements_by_css_selector(".rg_i.Q4LuWd")
for image in images: 
    try:
        image.click()
        time.sleep(2)        
        imgurl=driver.find_element_by_xpath('/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div/div[1]/div[1]/div/div[2]/a/img').get_attribute("src") 
        cookies = dict(BCPermissionLevel='PERSONAL')
        count+=1
        with open(str(count)+'.jpg','wb') as handle:
            response = requests.get(pic_url,headers={"User-Agent": "Mozilla/5.0"},cookies=cookies,stream=True)
            if not response.ok:
                    print (response)
            for block in response.iter_content(1024):
                    if not block:
                            break
                    handle.write(block)
        time.sleep(2)
    except:
        pass

如果您需要完整的代码来回答这里请。 https://github.com/yeon333/crawl-download-ing/blob/master/combate.py

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)