是白白 2021-10-12 19:58 采纳率: 100%
浏览 30
已结题

用selenium配合requests下载的图片一直被覆盖掉怎么才能让它不被覆盖

原本想整活弄点福利但是老是被覆盖

import random
import os #创建文件
from  selenium import webdriver
import requests
from lxml import etree
from selenium.webdriver import ChromeOptions   #这个包用来规避被检测的风险
import time
from  urllib import request
import pandas as pd

driver_path=r'C:\Users\哥斯拉\AppData\Local\Google\Chrome\Application\chromedriver.exe' #定义好路径
#这两段代码规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])

driver=webdriver.Chrome(executable_path=driver_path,options=option)  #初始化路径+规避检测

driver.get('https://www.enterdesk.com/search/1-0-7-0-0-0/%E9%BB%91%E4%B8%9D')
js='document.documentElement.scrollTop=10000'
driver.execute_script(js)
time.sleep(2)
driver.execute_script(js)
# driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")  #滚动到底部,但是如果是AJX的交互的没法滚到底部只能滚一点
p=0

def simulation_operation():
    souce=driver.page_source
    listpages=driver.find_element_by_xpath('//div[@class="listpages"]') #最底部
    get_img(souce)
    time.sleep(round(random.uniform(1, 4), 2))

    while True:
        driver.execute_script(js)
        if "listpages" in listpages.get_attribute("class"):
            break
        else:
            driver.execute_script(js)
#循环拉动滚轮到页面底部
def get_img(url):
    html=etree.HTML(url)
    imgs=html.xpath('//div[@class="egeli_pic_li"]//img/@src')
    titles=html.xpath('//div[@class="egeli_pic_li"]//img/@title')
    for title in titles:
        kk=title
    for img in imgs:
        page=requests.get(img).content
        # tt=page.content
        with open('{}.jpg'.format(kk), mode='wb') as f:
            f.write(page)





if __name__ == '__main__':
    simulation_operation()


这是全部的源代码,我也查过资料说可能是文件名重复了,可我用下面代码已经获取了文件名也赋值了啊

 imgs=html.xpath('//div[@class="egeli_pic_li"]//img/@src')     #获取图片的url
    titles=html.xpath('//div[@class="egeli_pic_li"]//img/@title')  #获取到图片的名称
    for title in titles:
        kk=title
    for img in imgs:
        page=requests.get(img).content
        # tt=page.content
        with open('{}.jpg'.format(kk), mode='wb') as f:
            f.write(page)

麻烦帮忙看看是哪里1有问题,可以的话帮忙完善一下代码,主要就卡在下载图片这里了

  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2021-10-12 23:28
    关注

    代码中这句for title in titles: kk=title,在循环外得到的kk是titles列表中的最后一个值,它是固定的名称,因而图片覆盖,这段get_img函数代码改成如下即可:

    def get_img(url):
        html = etree.HTML(url)
        imgs = html.xpath('//div[@class="egeli_pic_li"]//img/@src')
        titles = html.xpath('//div[@class="egeli_pic_li"]//img/@title')
        try:
            for title,img in zip(titles,imgs):
                page = requests.get(img).content
                # tt=page.content
                with open('imgs/{}.jpg'.format(title), mode='wb') as f:
                    f.write(page)
        except:
            pass
    

    如有帮助,请采纳。点击我回答右上角【采纳】按钮。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月21日
  • 已采纳回答 10月13日
  • 创建了问题 10月12日

悬赏问题

  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器