您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

5b51 2022/1/14 8:24:30 python 字数 15578 阅读 631 来源 www.jb51.cc/python

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

概述

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

爬虫分析

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

www.moko.cc/post/da39db…

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

获取所有列表页

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

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

进群:960410445  即可获取惊喜大礼包!嘿嘿 你私信一下就行!

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

快速的编写一个正则表达式

import requests
import 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.text
pattern = re.compile('<p class="title"><a hidefocus="ture".?href="(.?)" class="mwC u">.*?((d+?))

')

获取page_list

page_list = pattern.findall(page_text)

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.text
pattern = re.compile('<p class="title"><a hidefocus="ture".?href="(.?)" class="mwC u">.*?((d+?))

page_list = pattern.findall(page_text)

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.text
pattern = re.compile('<p class="title"><a hidefocus="ture".?href="(.?)" class="mwC u">.*?((d+?))

page_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"的产生,需要过滤掉

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

www.moko.cc/post/da39db…

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

http://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/1.html
http://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/2.html
http://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/3.html
http://www.moko.cc/post/da39db43246047c79dcaef44c201492d/category/304475/4.html

page_count = math.ceil(int(totle)/28)+1
for i in range(1,page_count):

正则表达式进行替换

pages = re.sub(r'd+?.html',str(i)+".html",start_page)
all_pages.append(base_url.format(pages))

page_count = math.ceil(int(totle)/28)+1
for i in range(1,page_count):

pages = re.sub(r'd+?.html',str(i)+".html",start_page)
all_pages.append(base_url.format(pages))

page_count = math.ceil(int(totle)/28)+1
for i in range(1,page_count):

pages = re.sub(r'd+?.html',str(i)+".html",start_page)
all_pages.append(base_url.format(pages))

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

# 获取所有的页面
def get_all_list_page(start_page,totle):
 page_count = math.ceil(int(totle)/28)+1
 for i in range(1,page_count):
 pages = re.sub(r'd+?.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+条数据

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

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

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

def read_list_data():
 # 读取数据
 img_list = pd.read_csv("./pages.csv",names=["no","url"])["url"]
 # 循环操作数据
 for img_list_page in img_list:
 try:
 response = requests.get(img_list_page,timeout=3)
 except Exception as e:
 print(e)
 continue
 # 正则表达式获取图片列表页面
 pattern = re.compile('VIEW MORE')
 img_Box = pattern.findall(response.text)
 need_links = [] # 待抓取的图片文件夹
 for img in img_Box:
 need_links.append(img)
 # 创建目录
 file_path = "./downs/{}".format(str(img[0]).replace('/',''))
 if not os.path.exists(file_path):
 os.mkdir(file_path) # 创建目录
 for need in need_links:
    # 获取详情页面图片链接
 get_my_imgs(base_url.format(need[1]),need[0])

上面代码几个重点地方

pattern = re.compile('VIEW MORE')
 img_Box = pattern.findall(response.text)
 need_links = [] # 待抓取的图片文件夹
 for img in img_Box:
 need_links.append(img)

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

# 创建目录
 file_path = "./downs/{}".format(str(img[0]).replace('/',''))
 if not os.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,timeout=3)
 pattern = re.compile('')
 all_imgs = pattern.findall(response.text)
 for download_img in all_imgs:
 downs_imgs(download_img,title)

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

def downs_imgs(img,title):
 headers ={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,timeout=3)
 content = response.content
 file_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("完毕")

运行代码,等着收图

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

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

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

Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐

代码中需要完善的地方

总结

以上是编程之家为你收集整理的Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐全部内容,希望文章能够帮你解决Python爬虫教程——美空网未登录图片爬取!这个网站这么多小姐姐所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶