本人对于Python学习创建了一个小小的学习圈子,为各位提供了一个平台,大家一起来讨论学习Python。欢迎各位到来Python学习群:960410445一起讨论视频分享学习。Python是未来的发展方向,正在挑战我们的分析能力及对世界的认知方式,因此,我们与时俱进,迎接变化,并不断的成长,掌握Python核心技术,才是掌握真正的价值所在。
爬虫分析
首先,我们已经爬取到了N多的用户个人主页,我通过链接拼接获取到了
在这个页面中,咱们要找几个核心的关键点,发现平面拍摄
点击进入的是图片列表页面。 接下来开始代码走起。
获取所有列表页面
我通过上篇博客已经获取到了70000(实际测试50000+)用户数据,读取到python中。
这个地方,我使用了一个比较好用的python库pandas,大家如果不熟悉,先模仿我的代码就可以了,我把注释都写完整。
import pandas as pd
# 用户图片列表页模板
user_list_url = "http://www.moko.cc/post/{}/list.html"
# 存放所有用户的列表页
user_profiles = []
def read_data():
# pandas从csv里面读取数据
df = pd.read_csv("./moko70000.csv") #文件在本文末尾可以下载
# 去掉昵称重复的数据
df = df.drop_duplicates(["nikename"])
# 按照粉丝数目进行降序
profiles = df.sort_values("follows", ascending=False)["profile"]
for i in profiles:
# 拼接链接
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 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+?)\)</a></p>')
# 获取page_list
page_list = pattern.findall(page_text)
运行得到结果
[('/post/da39db43246047c79dcaef44c201492d/category/304475/1.html', '85'), ('/post/da39db4324604
继续完善代码,我们发现上面获取的数据,有"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.text
pattern = re.compile('<p class="title"><a hidefocus="ture".*?href="(.*?)" class="mwC u">.*?\((\d+?)\)</a></p>')
# 获取page_list
page_list = pattern.findall(page_text)
# 过滤数据
for page in page_list:
if page[1] == '0':
page_list.remove(page)
print(page_list)
获取到列表页的入口,下面就要把所有的列表页面全部拿到了,这个地方需要点击下面的链接查看一下
本页面有分页,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))
当我们回去到足够多的链接之后,对于初学者,你可以先干这么一步,把这些链接存储到一个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',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,好吧,我承认这是给新手写的其实就是懒