#导入相应模块
import urllib.request
import re
import gevent
from gevent import monkey
monkey.patch_all()
def downloader(img_name, img_url):
#测试print(img_url)
req = urllib.request.urlopen(img_url)
img_content = req.read()
print(img_content)
with open("xiazai/%s"%img_name, "wb") as f:
f.write(img_content)
def main():
url_content = None
#网页源代码huya.txt
with open("huya.txt", "r") as f:
url_content = f.read()
#数据清洗
img = re.findall(r"https://huya.*?\.jpg", url_content)
print(img)
j=0
list1=list()
#while j <len(img):
#创建卵
for i in img:
#print(i)
spawn = gevent.spawn(downloader, str(j)+".jpg", i)
list1.append(spawn)
j += 1
#break
gevent.joinall(list1)
if __name__ == '__main__':
main()