Python爬虫新手入门教学(九):多线程爬虫案例讲解 待我称王封你为后i 2022-10-24 01:39 169阅读 0赞 # 前言 # 本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。 **Python爬虫、数据分析、网站开发等案例教程视频免费在线观看** https://space.bilibili.com/523606542 # ![a2b6984089fbb14c9cf0ff607a09682a.png][] # # 前文内容 # [Python爬虫新手入门教学(一):爬取豆瓣电影排行信息][Python] [Python爬虫新手入门教学(二):爬取小说][Python 1] [Python爬虫新手入门教学(三):爬取链家二手房数据][Python 2] [Python爬虫新手入门教学(四):爬取前程无忧招聘信息][Python 3] [Python爬虫新手入门教学(五):爬取B站视频弹幕][Python_B] [Python爬虫新手入门教学(六):制作词云图][Python 4] [Python爬虫新手入门教学(七):爬取腾讯视频弹幕][Python 5] [Python爬虫新手入门教学(八):爬取论坛文章保存成PDF][Python_PDF] # 基本开发环境 # * Python 3.6 * Pycharm * wkhtmltopdf # 相关模块的使用 # * re * requests * concurrent.futures 安装Python并添加到环境变量,pip安装需要的相关模块即可。 # 一、明确需求 # 现在聊天谁还不发几个表情包?聊天时,表情包是我们重要的工具,更是拉进小伙伴们距离的好帮手,当聊天陷入尴尬境地时,随手一张表情包,让尴尬化为无形 本篇文章就用python批量爬取表情包图片,留以备用 ![Python爬虫新手入门教学(九):多线程爬虫案例讲解][Python 6] # 二、网页数据分析 # ![Python爬虫新手入门教学(九):多线程爬虫案例讲解][Python 7] 如图所示斗图网上面的图片数据都包含在 a 标签当中,可以尝试直接请求这个网页,查看response 返回的数据当中是否也含有 图片地址。 import requests def get_response(html_url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response def main(html_url): response = get_response(html_url) print(response.text) if __name__ == '__main__': url = 'https://www.doutula.com/photo/list/' main(url) 在输出结果中 **ctrl + F** 进行搜索。 ![Python爬虫新手入门教学(九):多线程爬虫案例讲解][Python 8] 这里有一个点想要注意一下,我用python请求网页所给我们返回的结果当中,包含图片url地址是: **data-original="图片url"** **data-backup="图片url"** 如果想要提取url地址的话,可以用parsel 解析库,或者 re 正则表达式。之前都是使用的parsel,本篇文章就用 正则表达式吧。 urls = re.findall('data-original="(.*?)"', response.text) # 单页爬取完整代码 # import requests import re def get_response(html_url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response def save(image_url, image_name): image_content = get_response(image_url).content filename = 'images\\' + image_name with open(filename, mode='wb') as f: f.write(image_content) print(image_name) def main(html_url): response = get_response(html_url) urls = re.findall('data-original="(.*?)"', response.text) for link in urls: image_name = link.split('/')[-1] save(link, image_name) if __name__ == '__main__': url = 'https://www.doutula.com/photo/list/' main(url) # 多线程爬取全站图片(如果你的内存够大) # ![Python爬虫新手入门教学(九):多线程爬虫案例讲解][Python 9] 3631页的数据,什么表情都有,嘿嘿嘿 import requests import re import concurrent.futures def get_response(html_url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } response = requests.get(url=html_url, headers=headers) return response def save(image_url, image_name): image_content = get_response(image_url).content filename = 'images\\' + image_name with open(filename, mode='wb') as f: f.write(image_content) print(image_name) def main(html_url): response = get_response(html_url) urls = re.findall('data-original="(.*?)"', response.text) for link in urls: image_name = link.split('/')[-1] save(link, image_name) if __name__ == '__main__': # ThreadPoolExecutor 线程池的对象 # max_workers 最大任务数 executor = concurrent.futures.ThreadPoolExecutor(max_workers=3) for page in range(1, 3632): url = f'https://www.doutula.com/photo/list/?page={page}' # submit 往线程池里面添加任务 executor.submit(main, url) executor.shutdown() [a2b6984089fbb14c9cf0ff607a09682a.png]: /images/20221024/7a3b01da1bdd45c995c0e120f0e679cb.png [Python]: https://blog.csdn.net/m0_48405781/article/details/113055904 [Python 1]: https://blog.csdn.net/m0_48405781/article/details/113118597 [Python 2]: https://blog.csdn.net/m0_48405781/article/details/113140580 [Python 3]: https://blog.csdn.net/m0_48405781/article/details/113179478 [Python_B]: https://blog.csdn.net/m0_48405781/article/details/113183911?spm=1001.2014.3001.5501 [Python 4]: https://blog.csdn.net/m0_48405781/article/details/113247830 [Python 5]: https://blog.csdn.net/m0_48405781/article/details/113250671 [Python_PDF]: https://blog.csdn.net/m0_48405781/article/details/113336829 [Python 6]: /images/20221024/4cfe9d155d2d46b2969aff482be1d31c.png [Python 7]: /images/20221024/5557a467e4244969bdb28c54825f63a3.png [Python 8]: /images/20221024/d0e418acc17342c18c77ba58938f8e4a.png [Python 9]: /images/20221024/a12ad4ec124640ac8641befee71543b4.png
还没有评论,来说两句吧...