Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

协程运行速度比多进程慢 #18

Open
edmund-zhao opened this issue Mar 6, 2021 · 0 comments
Open

协程运行速度比多进程慢 #18

edmund-zhao opened this issue Mar 6, 2021 · 0 comments

Comments

@edmund-zhao
Copy link

@trip.coroutine
def downloadImg():
    if not os.path.exists('./data'):
        os.mkdir('./data')
    q = 0
    for i in downUrl:
#        if q < 1100:
#        	q += 1
#        	continue
        imgUrl = []
        lenOfData = 0
        r = yield s.get(i[0])
        Soup = BeautifulSoup(r.text,'html.parser')
        u = Soup.select('#main > article > div > div')
        if not os.path.exists('./data/{}'.format(i[-1])):
            os.mkdir('./data/{}'.format(i[-1]))
        else:
            lenOfData = len(os.listdir('./data/{}'.format(i[-1])))
        for t in u:
            if t.select('a') == []:
                continue
            imgUrl.append(t.select('a')[0].get('href'))
        if len(imgUrl) == lenOfData:
            print("跳过< {} >".format(i[-1]))
            continue
        t = 0
        for url in imgUrl:
            r = yield s.get(url,stream=True)
            with open('./data/{}/{}.jpg'.format(i[-1],t), 'wb') as fd:
                for chunk in r.iter_content(256):
                    chunk = yield chunk
                    fd.write(chunk)
            print("正在下载<{}>的第{}张图片".format(i[-1],t))
            t += 1
  • 函数如上,全局变量downUrl 是一个数组,保存Url连接的数组
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant