Python骚操作:爬虫爬取性感美女图,已装满我硬盘! - Go语言中文社区

Python骚操作:爬虫爬取性感美女图,已装满我硬盘!


作为一名兢兢业业的猿,我们的工作就是写代码and找Bug,这种技术活周围的妹子比较少,为了防止工作乏味有了程序员鼓励师这个职业,当然并不是所有的公司都会给我们配备,这个时候我们就需要用自己的长处“自己创造”!

如果你对编程感兴趣或者想往编程方向发展,可以关注微信公众号【筑梦编程】,大家一起交流讨论!小编也会每天定时更新既有趣又有用的编程知识!

GitHub:https://github.com/injetlee/Python/blob/master/%E7%88%AC%E8%99%AB%E9%9B%86%E5%90%88/meizitu.py

本文目标

  1. 熟悉 Requests 库,Beautiful Soup 库

  2. 熟悉多线程爬取

  3. 送福利,妹子图

先来看看爬虫爬取的成果:

 

其实这个项目去年就写好了,只是自己先消化一下,所以今天才放出来给大家看看!

网站结构

爬取的目标网址:http://meizitu.com/a/more_1.html 

进去后就如同下图所示:

 

这种图示一组一组的套图,点击一张图片可以进入这些套图的里面,大概一组套图有10张左右的图片,这些图还是1字排开的!

实现思路

看了界面的结构,那么我们的思路就有了。

  1. 构造几个URL链接,然后请求上面套图的列表,获取到每个页面的套图列表!

  2. 进入每一组套图里面,然后获取每组套图里的每张图片!

废话不多说,直接上代码了

  1. 下载界面的函数,利用 Requests 很方便实现。

def download_page(url): ''' 用于下载页面 ''' headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"} r = requests.get(url, headers=headers) r.encoding = 'gb2312' return r.text
  1. 获取套图的列表,Link是套图的链接(应该注释一下),text是套图的名字!

def get_pic_list(html):

'''

获取每个页面的套图列表,之后循环调用get_pic函数获取图片

'''

soup = BeautifulSoup(html, 'html.parser')pic_list = soup.find_all('li', class_='wp-item')for i in pic_list:a_tag = i.find('h3', class_='tit').find('a')link = a_tag.get('href') # 套图链接text = a_tag.get_text() # 套图名字get_pic(link, text)

  1. 传入上一步中获取到的套图链接及套图名字,获取每组套图里面的图片,并保存,我在代码中注释了。

def get_pic(link, text): ''' 获取当前页面的图片,并保存 ''' html = download_page(link) # 下载界面 soup = BeautifulSoup(html, 'html.parser') pic_list = soup.find('div', id="picture").find_all('img') # 找到界面所有图片 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"} create_dir('pic/{}'.format(text)) for i in pic_list: pic_link = i.get('src') # 拿到图片的具体 url r = requests.get(pic_link, headers=headers) # 下载图片,之后保存到文件 with open('pic/{}/{}'.format(text, pic_link.split('/')[-1]), 'wb') as f: f.write(r.content) time.sleep(1)

完整代码

完整代码放在下面,利用多线程去爬取(其实也不多,5个罢了)

import requestsimport osimport timeimport threadingfrom bs4 import BeautifulSoupdef download_page(url): ''' 用于下载页面 ''' headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"} r = requests.get(url, headers=headers) r.encoding = 'gb2312' return r.textdef get_pic_list(html): ''' 获取每个页面的套图列表,之后循环调用get_pic函数获取图片 ''' soup = BeautifulSoup(html, 'html.parser') pic_list = soup.find_all('li', class_='wp-item') for i in pic_list: a_tag = i.find('h3', class_='tit').find('a') link = a_tag.get('href') text = a_tag.get_text() get_pic(link, text)def get_pic(link, text): ''' 获取当前页面的图片,并保存 ''' html = download_page(link) # 下载界面 soup = BeautifulSoup(html, 'html.parser') pic_list = soup.find('div', id="picture").find_all('img') # 找到界面所有图片 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"} create_dir('pic/{}'.format(text)) for i in pic_list: pic_link = i.get('src') # 拿到图片的具体 url r = requests.get(pic_link, headers=headers) # 下载图片,之后保存到文件 with open('pic/{}/{}'.format(text, pic_link.split('/')[-1]), 'wb') as f: f.write(r.content) time.sleep(1) # 休息一下,不要给网站太大压力,避免被封def create_dir(name): if not os.path.exists(name): os.makedirs(name)def execute(url): page_html = download_page(url) get_pic_list(page_html)def main(): create_dir('pic') queue = [i for i in range(1, 72)] # 构造 url 链接 页码。 threads = [] while len(queue) > 0: for thread in threads: if not thread.is_alive(): threads.remove(thread) while len(threads) < 5 and len(queue) > 0: # 最大线程数设置为 5 cur_page = queue.pop(0) url = 'http://meizitu.com/a/more_{}.html'.format(cur_page) thread = threading.Thread(target=execute, args=(url,)) thread.setDaemon(True) thread.start() print('{}正在下载{}页'.format(threading.current_thread().name, cur_page)) threads.append(thread)if __name__ == '__main__': main()

好了,之后运行,我们的爬虫就会孜孜不倦的以5倍的效率为我们下载漂亮妹子的图片啦。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_44811417/article/details/90141265
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 17:40:57
  • 阅读 ( 1726 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢