python爬取美空网女神图片,小心你的内存走火

爬虫分析

首先,我们已经爬取到了N多的用户个人主页,我通过链接拼接获取到了

www.moko.cc/post/da39db…

在这个页面中,咱们要找几个核心的关键点,发现 平面拍摄 点击进入的是图片列表页面。 接下来开始代码走起。

获取所有列表页面

我通过上篇博客已经获取到了70000(实际测试50000+)用户数据,读取到python中。

这个地方,我使用了一个比较好用的python库pandas,大家如果不熟悉,先模仿我的代码就可以了,我把注释都写完整。

importpandasaspd# 用户图片列表页模板user_list_url ="http://www.moko.cc/post/{}/list.html"# 存放所有用户的列表页user_profiles = []defread_data():# pandas从csv里面读取数据df = pd.read_csv("./moko70000.csv")#文件在本文末尾可以下载# 去掉昵称重复的数据df = df.drop_duplicates(["nikename"])# 按照粉丝数目进行降序profiles = df.sort_values("follows", ascending=False)["profile"]foriinprofiles:# 拼接链接user_profiles.append(user_list_url.format(i))if__name__ =='__main__': read_data() print(user_profiles)复制代码

数据已经拿到,接下来我们需要获取图片列表页面,找一下规律,看到重点的信息如下所示,找对位置,就是正则表达式的事情了。

快速的编写一个正则表达式 <p class="title"><a hidefocus="ture".*?href="(.*?)" class="mwC u">.*?\((\d+?)\)</a></p> 引入re,requests模块

import requestsimport re复制代码# 获取图片列表页面def get_img_list_page():# 固定一个地址,方便测试test_url ="http://www.moko.cc/post/da39db43246047c79dcaef44c201492d/list.html"response = requests.get(test_url,headers=headers,timeout=3) page_text = response.textpattern = re.compile('.*?\((\d+?)\)')# 获取page_listpage_list = pattern.findall(page_text)复制代码

运行得到结果

[('/post/da39db43246047c79dcaef44c201492d/category/304475/1.html', '85'), ('/post/da39db43246047c79dcaef44c201492d/category/304476/1.html', '2'), ('/post/da39db43246047c79dcaef44c201492d/category/304473/1.html', '0')]复制代码

继续完善代码,我们发现上面获取的数据,有"0"的产生,需要过滤掉

# 获取图片列表页面def get_img_list_page():# 固定一个地址,方便测试test_url ="http://www.moko.cc/post/da39db43246047c79dcaef44c201492d/list.html"response = requests.get(test_url,headers=headers,timeout=3) page_text = response.textpattern = re.compile('<p class="title"><a hidefocus="ture".*?href="(.*?)" class="mwC u">.*?\((\d+?)\)</a></p>')# 获取page_listpage_list = pattern.findall(page_text)# 过滤数据forpageinpage_list:ifpage[1] =='0':page_list.remove(page) print(page_list)复制代码

获取到列表页的入口,下面就要把所有的列表页面全部拿到了,这个地方需要点击下面的链接查看一下

www.moko.cc/post/da39db…

本页面有分页,4页,每页显示数据 4*7=28 条 所以,基本计算公式为 math.ceil(85/28) 接下来是链接生成了,我们要把上面的链接,转换成

http://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/1.htmlhttp://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/2.htmlhttp://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/3.htmlhttp://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/4.html复制代码page_count = math.ceil(int(totle)/28)+1foriinrange(1,page_count):# 正则表达式进行替换pages = re.sub(r'\d+?\.html',str(i)+".html",start_page) all_pages.append(base_url.format(pages))复制代码

当我们回去到足够多的链接之后,对于初学者,你可以先干这么一步,把这些链接存储到一个csv文件中,方便后续开发

# 获取所有的页面defget_all_list_page(start_page,totle):page_count = math.ceil(int(totle)/28)+1foriinrange(1,page_count): pages = re.sub(r'\d+?\.html',str(i)+".html",start_page) all_pages.append(base_url.format(pages)) print("已经获取到{}条数据".format(len(all_pages)))if(len(all_pages)>1000): pd.DataFrame(all_pages).to_csv("./pages.csv",mode="a+") all_pages.clear()复制代码

让爬虫飞一会,我这边拿到了80000+条数据

好了,列表数据有了,接下来,我们继续操作这个数据,是不是感觉速度有点慢,代码写的有点LOW,好吧,我承认这是给新手写的 其实就是懒 ,我回头在用一篇文章把他给改成面向对象和多线程的

我们接下来基于爬取到的数据再次进行分析

例如 www.moko.cc/post/nimusi… 这个页面中,我们需要获取到,红色框框的地址,为什么要或者这个?因为点击这个图片之后进入里面才是完整的图片列表。

我们还是应用爬虫获取 几个步骤

循环我们刚才的数据列表

抓取网页源码

正则表达式匹配所有的链接

def read_list_data():# 读取数据img_list = pd.read_csv("./pages.csv",names=["no","url"])["url"]# 循环操作数据forimg_list_pageinimg_list:try: response = requests.get(img_list_page,headers=headers,timeout=3) except Exceptionase: print(e)continue# 正则表达式获取图片列表页面pattern = re.compile('<a hidefocus="ture" alt="(.*?)".*? href="(.*?)".*?>VIEW MORE</a>')img_box = pattern.findall(response.text) need_links = []# 待抓取的图片文件夹forimginimg_box: need_links.append(img)# 创建目录file_path ="./downs/{}".format(str(img[0]).replace('/', ''))ifnotos.path.exists(file_path): os.mkdir(file_path)# 创建目录forneedinneed_links:# 获取详情页面图片链接get_my_imgs(base_url.format(need[1]), need[0])复制代码

上面代码几个重点地方

pattern = re.compile('<a hidefocus="ture" alt="(.*?)".*? href="(.*?)".*?>VIEW MORE</a>')img_box = pattern.findall(response.text) need_links = []# 待抓取的图片文件夹forimginimg_box: need_links.append(img)复制代码

获取到抓取目录,这个地方,我匹配了两个部分,主要用于创建文件夹 创建文件夹需要用到 os 模块,记得导入一下

# 创建目录file_path ="./downs/{}".format(str(img[0]).replace('/',''))ifnotos.path.exists(file_path): os.mkdir(file_path)# 创建目录复制代码

获取到详情页面图片链接之后,在进行一次访问抓取所有图片链接

#获取详情页面数据def get_my_imgs(img,title): print(img)headers= {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"}response= requests.get(img,headers=headers,timeout=3)pattern= re.compile('

最后编写一个图片下载的方法,所有的代码完成,图片保存本地的地址,用的是时间戳。

def downs_imgs(img,title): headers ={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"} response = requests.get(img,headers=headers,timeout=3)content= response.contentfile_name= str(int(time.time()))+".jpg"file ="./downs/{}/{}".format(str(title).replace('/','').strip(),file_name) with open(file,"wb+") as f: f.write(content)print("完毕")复制代码

运行代码,等着收图

代码运行一下,发现报错了

原因是路径的问题,在路径中出现了...这个特殊字符,我们需要类似上面处理 / 的方式处理一下。自行处理一下吧。

数据获取到,就是这个样子的

代码中需要完善的地方

代码分成了两部分,并且是面向过程的,非常不好,需要改进

网络请求部分重复代码过多,需要进行抽象,并且加上错误处理,目前是有可能报错的

代码单线程,效率不高,可以参照前两篇文章进行改进

没有模拟登录,最多只能爬取6个图片,这也是为什么先把数据保存下来的原因,方便后期直接改造

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,629评论 19 139
  • 李畋先师 唐朝有个皇帝叫李世民,李世民有个宰相叫魏征。据说,魏征权力很大,"日管人间,夜辖阴曹"。一次,一个叫八河...
    沅沨阅读 1,482评论 0 1
  • 早上7点出发,出发地,佛山禅城,目的地百色
    随缘spring阅读 3,587评论 0 0
  • 在林依逢走后,陈精去了第八号当铺,想把莫飞的灵魂给释放出来。不料,邪恶的韩诺不仅不让离开,还欲强留她在当铺。 幸好...
    寒樰阅读 13,624评论 8 23
  • 原文连接 Abstract 虽然目前已经有比较高效的GBDT实现,如XGBoost和pGBRT,但是在特征维度很高...
    SSIGMA阅读 5,346评论 0 0