python爬虫爬取糗百成人图片多线程版本
来源:互联网 发布:ubuntu redis 绑定ip 编辑:程序博客网 时间:2024/04/30 15:45
来个简单的多线程,爬取速度比单线程要快很多,下面上代码:
#encoding:utf-8#多线程爬取import requestsimport osfrom bs4 import BeautifulSoupimport threadingimport urllib.requestFIRST_PAGE_URL = 'http://www.qiubaichengren.com/{}.html'PAGE_URL_LIST = []IMG_URL_LIST = [] #所有的图片链接NAME_LIST = []gLock = threading.Lock()for x in range(1,100): page_url = FIRST_PAGE_URL.format(x) PAGE_URL_LIST.append(page_url)def get_page(): while True: gLock.acquire() if len(PAGE_URL_LIST) == 0: gLock.release() break else: page_url = PAGE_URL_LIST.pop() gLock.release() response = requests.get(page_url) content = response.content soup = BeautifulSoup(content, 'lxml') src = soup.find_all('div', class_='mala-text') imgs = soup.find_all('img') for img in src: url = img.find('img') link = url.get('src') title = url.get('alt') split_list = link.split('/') final = split_list.pop() t_split_list = final.split('.') suffix = t_split_list.pop() filename = title + '.' + suffix # 名字加后缀 gLock.acquire() NAME_LIST.append(filename) IMG_URL_LIST.append(link) gLock.release() #download_image(link, filename)def download_image(): while True: gLock.acquire() if len(IMG_URL_LIST) == 0: gLock.release() continue else: url = IMG_URL_LIST.pop() filename = NAME_LIST.pop() gLock.release() path = os.path.join('images', filename) urllib.request.urlretrieve(url, filename=path)def main(): for x in range(4): th = threading.Thread(target=get_page) th.start() for x in range(5): th = threading.Thread(target=download_image) th.start()if __name__ == "__main__": main()
阅读全文
0 0
- python爬虫爬取糗百成人图片多线程版本
- python爬虫爬取糗百成人图片单线程版本
- python爬虫系列一:爬取糗百成人的妹子图片(urllib2)
- python爬虫系列二:爬取糗百成人的妹子图片(requests+正则)
- python爬虫系列三:爬取糗百成人的妹子图片(scrapy框架+正则)
- python多线程图片爬虫
- python多线程图片爬虫
- python 网页爬虫+保存图片+多线程+网络代理
- python 网页爬虫+保存图片+多线程+网络代理
- Python 爬虫多线程爬取美女图片保存到本地
- python下多线程爬虫爬取斗图网的所有最新图片
- Python爬虫:初探多线程爬虫
- Python小爬虫,(多线程)
- python多线程定向爬虫
- Python+多线程+队列爬虫
- Python多线程爬虫
- python多线程爬虫框架
- python中的多线程爬虫
- 《数字图像处理的MATLAB实现》读书报告
- 使用Java程序输出1~100之间 7的倍数的个数及总和,并打印输出
- HashMap底层实现原理
- PHP Notice: Undefined variable: _SESSION
- 自定义控件---save和saveLayler中参数的意义
- python爬虫爬取糗百成人图片多线程版本
- leetcode 338. Counting Bits
- linux下用rpm 安装jdk
- 数据结构-栈的顺序存储
- 网页静态化
- [发福利]APPx双11上线大放价,定制小程序只需一元
- 1-9·shell入门、脚本、概念
- 2017.11.07
- classpath拒绝访问