爬虫进阶
主要用到了进程池来加速
- 代码在这儿啦,相信优秀的你看代码就会懂了。
- 谨慎运行( = = !)
- 其中 PIL 模块 不存在的话就 win+R 打开cmd 装一下模块命令如下:
pip install Pillow -i https://pypi.douban.com/simple/
from PIL import Image
from io import BytesIO
import requests
import time
from multiprocessing import Pool
def get_res():
pool = Pool(10) # 每次打开图片的数量(进程数量)
while True:
time.sleep(5) # 每次打开图片间隔 10秒
try:
xs = pool.apply_async(func=show_img)
except Exception as e:
print("请求超时" + str(e))
def show_img():
url = "https://api.66mz8.com/api/rand.tbimg.php"
res = requests.get(url).content
b = BytesIO(res)
img = Image.open(b)
img.resize((50, 50), Image.ANTIALIAS)
img.show()
if __name__ == '__main__':
get_res()