關於爬蟲爬圖
最近自己看着網上教程學習如何爬圖,發現爬蟲的優越性,也發現有些博客對初學者不太友好,因此寫了這篇博客。
如何獲取網頁源代碼
打開谷歌瀏覽器,打開百度圖片,點擊更多工具—>打開開發者工具,如圖
可以發現,在你下劃的時候,旁邊Name一欄會出現很多圖片鏈接。
隨便點擊一個圖片,發現旁邊有一個關於Response Headers內容
這個就是你現在訪問百度圖片的訪問信息,而這一些信息其實都是大同小異的。
再左鍵網頁查看網頁源代碼。
再按Ctrl/Command + F,利用查找工具,查找url,即圖片的下載地址,還有一些相關信息,例如data等都可以很快找出,在代碼中有體現。
輸入frompagetitle可以查找到圖像的Title。
源代碼
源代碼中有詳細的註釋便於大家理解。
# 爬取百度圖片
from urllib.parse import urlencode
import requests
import re
import os
# 圖片下載的存儲文件
save_dir = '百度圖片/'
# 百度加密算法
def baidtu_uncomplie(url):
res = ''
c = ['_z2C$q', '_z&e3B', 'AzdH3F']
d = {'w': 'a', 'k': 'b', 'v': 'c', '1': 'd', 'j': 'e', 'u': 'f', '2': 'g', 'i': 'h', 't': 'i', '3': 'j', 'h': 'k',
's': 'l', '4': 'm', 'g': 'n', '5': 'o', 'r': 'p', 'q': 'q', '6': 'r', 'f': 's', 'p': 't', '7': 'u', 'e': 'v',
'o': 'w', '8': '1', 'd': '2', 'n': '3', '9': '4', 'c': '5', 'm': '6', '0': '7', 'b': '8', 'l': '9', 'a': '0',
'_z2C$q': ':', '_z&e3B': '.', 'AzdH3F': '/'}
if (url == None or 'http' in url): # 判斷地址是否有http
return url
else:
j = url
# 解碼百度加密算法
for m in c:
j = j.replace(m, d[m])
for char in j:
if re.match('^[a-w\d]+$', char): # 正則表達式
char = d[char]
res = res + char
return res
# 獲取頁面信息
def get_page(offset):
params = {
'tn': 'resultjson_com',
'ipn': 'rj',
'ct': '201326592',
'is': '',
'fp': 'result',
'queryWord': '中國人', # 關鍵字
'cl': '2',
'lm': '-1',
'ie': 'utf-8',
'oe': 'utf-8',
'adpicid': '',
'st': '-1',
'z': '',
'ic': '0',
'word': '中國人', # 關鍵字
's': '',
'se': '',
'tab': '',
'width': '',
'height': '',
'face': '0',
'istype': '2',
'qc': '',
'nc': '1',
'fr': '',
'expermode': '',
'pn': offset * 30,
'rn': '30',
'gsm': '1e',
'1537355234668': '',
}
url = 'https://image.baidu.com/search/acjson?' + urlencode(params)
try: # 嘗試連接服務器
response = requests.get(url)
if response.status_code == 200: # 獲取HTTP狀態,即服務器響應HTTP請求
return response.json()
except requests.ConnectionError as d:
print('Error', d.args)
# 獲取圖像
def get_images(json):
if json.get('data'):
for item in json.get('data'): # 獲取圖片數據字典值
if item.get('fromPageTitle'): # 獲取圖片Title
title = item.get('fromPageTitle')
else:
title = 'noTitle'
image = baidtu_uncomplie(item.get('objURL')) # 圖片地址
if (image):
yield { # 存儲圖片信息
'image': image,
'title': title
}
def save_image(item, count):
try:
response = requests.get(item.get('image'))
if response.status_code == 200: # 獲取HTTP狀態,即服務器響應HTTP請求
file_path = save_dir + '{0}.{1}'.format(str(count), 'jpg') # 命名並存儲圖片
if not os.path.exists(file_path): # 判斷圖片是否在文件中
with open(file_path, 'wb') as f:
f.write(response.content)
else:
print('Already Downloaded', file_path)
except requests.ConnectionError: # 如果出現連接錯誤
print('Failed to Save Image')
def main(pageIndex, count):
json = get_page(pageIndex)
for image in get_images(json):
save_image(image, count)
count += 1
return count
if __name__ == '__main__':
if not os.path.exists(save_dir): # 判斷是否存在文件,若沒有則創建一個
os.mkdir(save_dir)
count = 1
for i in range(1, 200): # 循環頁數下載圖片
count = main(i, count) # i表示頁數,統計圖片並運行主函數
print('total:', count)