日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

python 爬取作品集_Python批量抓取站酷ZCOOL作品图片并归档

發(fā)布時(shí)間:2024/9/27 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 爬取作品集_Python批量抓取站酷ZCOOL作品图片并归档 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

前幾天,由于個(gè)人有需求,所以就要對(duì)站酷網(wǎng)一些類別下的作品的圖片進(jìn)行批量抓取,首先是采用的是NodeJs來寫的,但是在運(yùn)行的途中遇到很多的問題,所以后來就換成了Python,同時(shí)使用了多線程,使得圖片下載時(shí)達(dá)到了寬帶的峰值,同樣也保證了其質(zhì)量(首先聲明:此代碼僅供于學(xué)習(xí)交流使用,不能用于其他的任何用途)。

/>

正文

我本次采用的是request和BeautifulSoup組合來進(jìn)行抓取的,首先我們需要分析站酷的DOM結(jié)構(gòu):

/>

由上我們可以看出,其主要內(nèi)容包含在class為work-list-box的這個(gè)div內(nèi),并且每個(gè)作品又是單獨(dú)包含在class為card-box這個(gè)div內(nèi)的,所以我們需要獲取到頁(yè)面所有的作品元素集合,我封裝成了一個(gè)函數(shù):

def startRequest(url):

print("【提示】正在抓取 - %s " % url)

res = requests.get(url)

if res.status_code == 200:

res_html = res.content.decode()

doc = BeautifulSoup(res_html)

work_box = doc.find('div', class_={'work-list-box'})

card_box_list = work_box.find_all('div', class_={'card-box'}) # 此處為所有的作品元素集合

else:

print("【文檔獲取失敗】【狀態(tài)為%s】 - %s," % (url, res.status_code))

接下來分析每個(gè)模塊元素的結(jié)構(gòu),通過查看DOM元素得知每個(gè)card-box下的class為title-content的這個(gè)a標(biāo)簽包含了其作品的鏈接地址以及作品標(biāo)題,所以我們就可以由此來獲取:

title_content = item.find("a", class_={'title-content'})

avatar = item.find('span', class_={'user-avatar'})

但是在作品集合元素內(nèi),嵌套著推廣或廣告,但是推廣和廣告是沒有頭像avatar元素的,所以我們只需要這樣檢測(cè)頭像是否為空就可以過濾掉廣告元素,其后獲取到作者,URL鏈接以及標(biāo)題:

def getContent(item):

title_content = item.find("a", class_={'title-content'})

avatar = item.find('span', class_={'user-avatar'})

if title_content is not None and avatar is not None:

title = title_content.text

author = avatar.find("a")["title"]

href = title_content['href']

else:

return

接下來,我們需要訪問對(duì)應(yīng)的URL,并獲取到其中的URL地址:

/>

通過查看DOM元素結(jié)構(gòu)可以看出,其文章主要內(nèi)容區(qū)域在class為work-show-box的div下,然后每個(gè)圖片/視頻/文字區(qū)域都是放在class為reveal-work-wrap的div內(nèi),所以獲取到所有的內(nèi)容區(qū)域集合,然后查看是否有img標(biāo)簽,如果有的話就說明存在圖片元素,則是我們需要的內(nèi)容,并且獲取到其src存放至url的集合中去:

def getDocImgLinks(html):

doc = BeautifulSoup(html)

work_box = doc.find("div", class_={'work-show-box'})

revs = work_box.find_all("div", class_={'reveal-work-wrap'})

img_list = []

for item in revs:

img = item.find("img")

if img is not None:

img_url = img["src"]

img_list.append(img_url)

else:

print("【提示】:沒有圖片")

continue

return img_list

[v_tips]要注意的一點(diǎn)就是,其中有些圖片如上圖中,會(huì)存在@這個(gè)表示,就好比http://img.zcool.cn/community/0170165a372344a80121db8001781d.jpg@1280w_1l_2o_100sh.jpg,這個(gè)是將原圖片進(jìn)行了裁剪的縮略圖,我們只需要將@以及后面的內(nèi)容去掉就可以得到原圖,最后為:http://img.zcool.cn/community/0170165a372344a80121db8001781d.jpg[/v_tips]

最后,我們需要將獲取的URL圖片進(jìn)行存儲(chǔ),存儲(chǔ)的文件夾就以【作者名】-【作品標(biāo)題】為標(biāo)準(zhǔn),然后圖片就以UUID來隨機(jī)命名,同時(shí)我們需要對(duì)一些特殊字符進(jìn)行過濾轉(zhuǎn)換,以防創(chuàng)建文件夾或文件失敗:

def nameEncode(file_name):

file_stop_str = ['\\', '/', '*', '?', ':', '"', '', '|']

for item2 in file_stop_str:

file_name = file_name.replace(item2, '-')

return file_name

所以,最后的完整源碼如下:

import sys

from bs4 import BeautifulSoup

import requests

import os

import uuid

import threading

def startRequest(url):

print("【提示】正在抓取 - %s " % url)

res = requests.get(url)

if res.status_code == 200:

res_html = res.content.decode()

doc = BeautifulSoup(res_html)

work_box = doc.find('div', class_={'work-list-box'})

card_box_list = work_box.find_all('div', class_={'card-box'})

for item in card_box_list:

getContent(item)

else:

print("【文檔獲取失敗】【狀態(tài)為%s】 - %s," % (url, res.status_code))

def getContent(item):

title_content = item.find("a", class_={'title-content'})

avatar = item.find('span', class_={'user-avatar'})

if title_content is not None and avatar is not None:

title = title_content.text

author = avatar.find("a")["title"]

href = title_content['href']

# print("%s - 【%s】- %s" % (title, author, href))

res = requests.get(href)

if res.status_code == 200:

# 獲取所有的圖片鏈接

img_list = getDocImgLinks(res.content.decode())

path_str = "【%s】-【%s】" % (author, title)

path_str_mk = pathBase('./data/'+nameEncode(path_str))

if path_str_mk is None:

return

else:

for img_item in img_list:

downloadImg(img_item, path_str_mk)

else:

print("【文檔獲取失敗】【狀態(tài)為%s】 - %s," % (href, res.status_code))

else:

return

def getDocImgLinks(html):

doc = BeautifulSoup(html)

work_box = doc.find("div", class_={'work-show-box'})

revs = work_box.find_all("div", class_={'reveal-work-wrap'})

img_list = []

for item in revs:

img = item.find("img")

if img is not None:

img_url = img["src"]

img_list.append(img_url)

else:

print("【提示】:沒有圖片")

continue

return img_list

def pathBase(file_path):

file_name_s = file_path.split("/")

file_name = file_name_s[len(file_name_s) - 1]

file_name_s[len(file_name_s) - 1] = file_name

path = "/".join(file_name_s)

if not os.path.exists(path):

os.mkdir(path)

return path

def nameEncode(file_name):

file_stop_str = ['\\', '/', '*', '?', ':', '"', '', '|']

for item2 in file_stop_str:

file_name = file_name.replace(item2, '-')

return file_name

def downloadImg(url, path):

z_url = url.split("@")[0]

hz = url.split(".")

z_hz = hz[len(hz) - 1]

res = requests.get(z_url)

if res.status_code == 200:

img_down_path = path + "/" + str(uuid.uuid1()) + "." + z_hz

f = open(img_down_path, 'wb')

f.write(res.content)

f.close()

print("【下載成功】 - %s" % img_down_path)

else:

print("【IMG下載失敗】【狀態(tài)為%s】 - %s," % (z_url, res.status_code))

if __name__ == '__main__':

threads = []

for i in range(1, 22):

url = 'http://www.zcool.com.cn/search/content?type=3&field=8&other=0&sort=5&word=APP%E8%AE%BE%E8%AE%A1' \

'&recommend=0&requestId=requestId_1513228221822&p='+(str(i))+'#tab_anchor '

threads.append(threading.Thread(target=startRequest, args={url}))

for item in threads:

item.start()

后記

如果上述代碼中有任何不清楚的地方,您可以在下方進(jìn)行留言,我會(huì)及時(shí)回答您的疑惑。

總結(jié)

以上是生活随笔為你收集整理的python 爬取作品集_Python批量抓取站酷ZCOOL作品图片并归档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。