我如何从该网站获取图像?

时间:2019-01-07 20:46:26

标签: python parsing python-requests

我在第7行有错误。 我只在此主机(https://prnt.sc/)上遇到麻烦,遇到下一个错误: requests.exceptions.SSLError:发生了违反协议(_ssl.c:777)的EOF

我尝试使用其他库,例如urllib和wget,同样遇到麻烦

import requests
from random import choice
alph = 'abcdefghijklmnopqrstuvwxyz1234567890'
url_start = 'm3sm4x'
url_finish = 'https://prnt.sc/'+url_start+'.img'
filename = 'D:\\imgur\\content\\'+url_finish.split('/')[-1]
img = requests.get(url_finish, stream=True)
n = 0
while n != 100:
    url_start = url_start.replace(choice(url_start),choice(alph))
    url_finish = 'https://prnt.sc/'+url_start+'.img'
    n+=1
    try:
        img = requests.get(url_finish, stream=True)
    except:
        print('invalid')
    if img.status_code == 200:
        with open('D:\\imgur\\content\\'+filename,' wb') as f:
            f.write(img)

2 个答案:

答案 0 :(得分:0)

我设法使用selenium下载图像:

import os.path
import urllib.request
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://prnt.sc/m3sm4x')
images = driver.find_elements_by_tag_name('img')
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
for image in images:
    img_url = image.get_attribute('src')
    file_name = os.path.basename(img_url)
    try:
        urllib.request.urlretrieve(img_url, file_name)
    except:
        pass
driver.close()

答案 1 :(得分:0)

我通过重新安装python决定了问题。