为了账号安全,请及时绑定邮箱和手机立即绑定

我需要使用 Python Selenium 下载锚的 href 属性中引用的图像

我需要使用 Python Selenium 下载锚的 href 属性中引用的图像

largeQ 2022-04-23 21:17:29
应以顺序方式获得检索到的链接,以便进行进一步处理。这是我到目前为止所尝试的:lay = driver.find_element_by_xpath('//*[@id="app"]/div/div[4]/div[2]/div/div[1]/div/div')fig = lay.find_elements_by_class_name('_2Mc8_')for link in fig:    href = link.get_attribute("href")    print href    for ab in href:           ab = driver.get(href)        dwn = driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/div/div[1]/div[1]/header/div[2]/div[3]/a/span')        dwn.click()        time.sleep(2) 
查看完整描述

2 回答

?
潇湘沐

TA贡献1816条经验 获得超6个赞

lay = driver.find_element_by_xpath('//')

fig = lay.find_elements_by_class_name('_2Mc8_')


for link in fig:

    href = link.get_attribute("href")

    print href

    for ab in href:   

        driver.get(ab)

        dwn = driver.find_element_by_xpath('//')

        dwn.click()

        time.sleep(2)


查看完整回答
反对 回复 2022-04-23
?
狐的传说

TA贡献1804条经验 获得超3个赞

前段时间我有一个这样的项目。您要做的是打开一种流并将对象下载到该流,然后再次关闭它。


def requests_image(file_url):

    i = requests.get(file_url)

    if i.status_code == requests.codes.ok:

        #save the file as a temporary name. Note that this is a static name, so I won't be able to run two threads at the same time.

        with iopen("images/TEMP_file_name", 'wb') as file:

            file.write(i.content)

            #this is to get the correct extension of the file. Handy when you can't derive it from the URL.

            ext = imghdr.what("images/TEMP_file_name")

            file.close()

            uidname = str(int(time.time()))[-8:]

            ##Create unique filename using UNIXTIME[-8:] (last 8 chars of unixtime in S)

            filename = uidname+"."+ext

            #Now that the stream is closed, rename it from the static name to a unique name. I chose to use time since epoch. Add the extension and you're good to go.

            os.rename("images/TEMP_file_name", "images/"+filename)

            return(filename)

    else:

        return False

以上是我要使用的功能。要调用它,只需执行以下操作:


fname = requests_image(href)

if fname: #truthy statement

    pass #if you have a succesfull file returned it might be needed to store it in a database. use the fname variable for this.

else:

    pass# if the filename returns false (might be if the link turns out to be invalid) log it and investigate if it's essential to know.


查看完整回答
反对 回复 2022-04-23
  • 2 回答
  • 0 关注
  • 142 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信