Python爬取美空網未登入圖片
本人對於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,好吧,我承認這是給新手寫的其實就是懶