Python爬蟲入門教程 4-100 美空網未登陸圖片爬取

簡介

上一篇寫的時間有點長了,接下來繼續把美空網的爬蟲寫完,這套教程中編寫的爬蟲在實際的工做中可能並不能給你增長多少有價值的技術點,由於它只是一套入門的教程,老鳥你自動繞過就能夠了,或者帶帶我也行。html

爬蟲分析

首先,咱們已經爬取到了N多的用戶我的主頁,我經過連接拼接獲取到了python

http://www.moko.cc/post/da39d...正則表達式

clipboard.png

在這個頁面中,我們要找幾個核心的關鍵點,發現平面拍攝點擊進入的是圖片列表頁面。
接下來開始代碼走起。網絡

獲取全部列表頁面

我經過上篇博客已經獲取到了70000(實際測試50000+)用戶數據,讀取到python中。 多線程

這個地方,我使用了一個比較好用的python庫pandas,你們若是不熟悉,先模仿個人代碼就能夠了,我把註釋都寫完整。app

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)

數據已經拿到,接下來咱們須要獲取圖片列表頁面,找一下規律,看到重點的信息以下所示,找對位置,就是正則表達式的事情了。ide

clipboard.png

快速的編寫一個正則表達式
<p class="title"><a hidefocus="ture".*?href="(.*?)" class="mwC u">.*?\((\d+?)\)</a></p>post

引入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)

運行獲得結果url

[('/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.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)

獲取到列表頁的入口,下面就要把全部的列表頁面所有拿到了,這個地方須要點擊下面的連接查看一下

http://www.moko.cc/post/da39d...

本頁面有分頁,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+條數據

clipboard.png

好了,列表數據有了,接下來,咱們繼續操做這個數據,是否是感受速度有點慢,代碼寫的有點LOW,好吧,我認可這是給新手寫的其實就是懶,我回頭在用一篇文章把他給改爲面向對象和多線程的

clipboard.png

咱們接下來基於爬取到的數據再次進行分析

例如 http://www.moko.cc/post/nimus... 這個頁面中,咱們須要獲取到,紅色框框的地址,爲何要或者這個?由於點擊這個圖片以後進入裏面纔是完整的圖片列表。

clipboard.png

咱們仍是應用爬蟲獲取
幾個步驟

  1. 循環咱們剛纔的數據列表
  2. 抓取網頁源碼
  3. 正則表達式匹配全部的連接
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,headers=headers,timeout=3)
        except Exception as e:
            print(e)
            continue
        # 正則表達式獲取圖片列表頁面
        pattern = re.compile('<a hidefocus="ture" alt="(.*?)".*? href="(.*?)".*?>VIEW MORE</a>')
        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('<a hidefocus="ture" alt="(.*?)".*? href="(.*?)".*?>VIEW MORE</a>')
        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, headers=headers, timeout=3)
    pattern = re.compile('<img src2="(.*?)".*?>')
    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, like Gecko) Chrome/68.0.3440.106 Safari/537.36"}
    response = requests.get(img,headers=headers,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("完畢")

運行代碼,等着收圖

圖片描述

代碼運行一下,發現報錯了

clipboard.png

緣由是路徑的問題,在路徑中出現了...這個特殊字符,咱們須要相似上面處理/的方式處理一下。自行處理一下吧。

數據獲取到,就是這個樣子的

clipboard.png

代碼中須要完善的地方

  1. 代碼分紅了兩部分,而且是面向過程的,很是很差,須要改進
  2. 網絡請求部分重複代碼過多,須要進行抽象,而且加上錯誤處理,目前是有可能報錯的
  3. 代碼單線程,效率不高,能夠參照前兩篇文章進行改進
  4. 沒有模擬登陸,最多隻能爬取6個圖片,這也是爲何先把數據保存下來的緣由,方便後期直接改造
相關文章
相關標籤/搜索