Skip to content

下载图片时,有时成功,有时失败 #15

Open
@xinheng-cqb

Description

@xinheng-cqb

图片能下载下来,运行也没报错,可是有些图片是能打开的,有些图片时损坏的,而且损坏的占了7层多,用的是最新版 0.0.10

import datetime
import trip
today_date = datetime.datetime.now().strftime('%Y-%m-%d')
headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
            'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
            'Referer': 'http://iir.circ.gov.cn/ipq/',
            'Connection': 'close',
            'content-type': 'application/json'
        }

@trip.coroutine
def download(i):
    try:
        s = trip.Session()
        res = yield s.get(
            'http://iir.circ.gov.cn/ipq/captchacn.svl?Mon%20May%2013%202019%2017:14:14%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)', stream=True, headers=headers,
                                                       timeout=25)
        file_name = 'E://captcha2//{0}_{1}.jpg'.format(today_date, i)
        if 200 == res.status_code:
            print (file_name)
            with open(file_name, 'wb') as fd:
                for chunk in res.iter_content(1024):
                    chunk = yield chunk
                    fd.write(chunk)
    except:
        import traceback
        print(traceback.format_exc())
def run():
    yield [download(i) for i in range(1, 50)]

trip.run(run)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions