diff --git "a/NO37/imgs/00\345\220\216\351\234\270\346\260\224\345\244\264\345\203\217\345\245\263\347\224\237\350\266\205\346\213\275\351\253\230\351\233\205 \345\277\230\345\215\264\351\202\243\344\272\233\346\265\201\350\250\200\350\234\232\350\257\255.jpg" "b/NO37/imgs/00\345\220\216\351\234\270\346\260\224\345\244\264\345\203\217\345\245\263\347\224\237\350\266\205\346\213\275\351\253\230\351\233\205 \345\277\230\345\215\264\351\202\243\344\272\233\346\265\201\350\250\200\350\234\232\350\257\255.jpg" new file mode 100644 index 0000000000000000000000000000000000000000..58c1608575a400dbab3ad7719be3d676ec59cc77 Binary files /dev/null and "b/NO37/imgs/00\345\220\216\351\234\270\346\260\224\345\244\264\345\203\217\345\245\263\347\224\237\350\266\205\346\213\275\351\253\230\351\233\205 \345\277\230\345\215\264\351\202\243\344\272\233\346\265\201\350\250\200\350\234\232\350\257\255.jpg" differ diff --git "a/NO37/\345\245\263\347\224\237\345\244\264\345\203\217\347\275\221\351\207\207\351\233\206.py" "b/NO37/\345\245\263\347\224\237\345\244\264\345\203\217\347\275\221\351\207\207\351\233\206.py" new file mode 100644 index 0000000000000000000000000000000000000000..2608661fca2f7aeec45d3b753c23ab71ae29e085 --- /dev/null +++ "b/NO37/\345\245\263\347\224\237\345\244\264\345\203\217\347\275\221\351\207\207\351\233\206.py" @@ -0,0 +1,49 @@ +from gevent import monkey + +monkey.patch_all() + +import threading +from bs4 import BeautifulSoup + +import gevent +import requests +import lxml + + +def get_page(this_urls): + while True: + + if this_urls is None: + break + url = this_urls.pop() + print('正在抓取:{},当前的虚拟线程为:{}'.format(url, threading.current_thread().getName())) + res = requests.get(url=url) + res.encoding = "gb2312" + if res.status_code == 200: + + soup = BeautifulSoup(res.text, 'lxml') + content = soup.find(attrs={'class': 'g-gxlist-imgbox'}) + img_tags = content.find_all('img') + + for img_tag in img_tags: + img_src = img_tag['src'] + # 注意去除文件路径中的特殊符号,防止出错 + try: + name = img_tag['alt'].replace('/', '').replace('+', '').replace('?', '').replace('*', '') + except OSError as e: + continue + save_img(img_src, name) + + +def save_img(img_src, name): + res = requests.get(img_src) + with open(f'imgs/{name}.jpg', mode='wb') as f: + f.write(res.content) + + +if __name__ == '__main__': + urls = [f"https://www.qqtn.com/tx/nvshengtx_{page}.html" for page in range(1, 244)] + # 开启 5 个协程 + gevent.joinall([gevent.spawn(get_page, urls) for i in range(5)]) + + print("爬取完毕") diff --git a/README.md b/README.md index 78598a1753ce3d1d8c5d0c0e4b8cfd93f966d7f6..baed5383373a33fde3fdf96a7c79aefebbb32356 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ ### 📙 协程学习 -37. 腾牛个性网QQ头像采集,gevent 模块 +37. [python 爬虫爱好者必须掌握的知识点“ 协程爬虫”,看一下如何用 gevent 采集女生用头像](https://dream.blog.csdn.net/article/details/120421824) 38. 麦涩网 MyCoser|cosplay 采集,asyncio 库学习 39. 中少绘本 MP4 视频采集,asyncio 协程第3篇 40. Bensound 站 MP3 采集,asyncio + aiohttp 协程第4篇