生活随笔
收集整理的這篇文章主要介紹了
爬虫百度图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
'''
爬蟲百度圖片
'''
import re
import os
import time
import requests
from selenium import webdriver#########################
###此段代碼不需要關(guān)心啥意思###
#########################if not os.path.exists('百度圖片'):os.mkdir('百度圖片')#####################
###限制30張圖片的代碼###
##################### # 獲取所有圖片
# response = requests.get(
# 'http://image.baidu.com/search/index?ct=201326592&cl=2&st=-1&lm=-1&nc=1&ie=utf-8&tn=baiduimage&ipn=r&rps=1&pv=&fm=rs7&word=美女')
# data = response.text
# img_desc_dics = re.findall("app.setData(\('imgData.*?\));", data, re.S)[0]
# img_desc_dics = eval(str(img_desc_dics))
#
# # 獲取所有圖片的數(shù)據(jù)
# img_datas = img_desc_dics[1]['data']
# count = 0
# for img_data in img_datas:
# # 獲取搜索圖片的參數(shù)
# os_ = img_data.get('os')
# cs_ = img_data.get('cs')
#
# if os_ and cs_:
# # 獲取搜索圖片的信息
# img_search_url = f'http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E9%A3%8E%E6%99%AF&step_word=&hs=0&pn=1&spn=0&di=195030&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs={cs_}&os={os_}'
# img_search_response = requests.get(img_search_url)
# img_search_data = img_search_response.text
#
# # 獲取圖片信息
# img_url = re.findall('''\('firstSc'\);" src="(.*?)"''', img_search_data)[0]
# img_name = img_url.split('/')[-1]
# img_name = os.path.join('百度圖片', img_name) # 拼接出圖片的地址,如 百度圖片/3822951_144045377000_2.jpg
#
# # 保存圖片
# img_response = requests.get(img_url)
# img_data = img_response.content
# fw = open(img_name, 'wb')
# fw.write(img_data)
# fw.flush()
#
# # 提示
# count += 1
# print(f'{img_name}保存成功,成功保存{count}張')
#
# # 防止百度禁ip,慢一點(diǎn)
# time.sleep(0.01)#########################################################################
###自行百度selenium的用法,使用這一套代碼可以無限爬取所有圖片,否則將被限制30張#### http://npm.taobao.org/mirrors/chromedriver/
######################################################################### pip install seleniumpage_count_end = 100 # 爬取 指定數(shù)字(10)* 30 = 300張圖片
word = '美女'
chrome = webdriver.Chrome()try:chrome.implicitly_wait(100)chrome.get(f'http://image.baidu.com/search/index?ct=201326592&cl=2&st=-1&lm=-1&nc=1&ie=utf-8&tn=baiduimage&ipn=r&rps=1&pv=&fm=rs7&word={word}')js_code = '''window.scrollTo(0, document.body.scrollHeight);var lenOfPage = document.body.scrollHeight;return lenOfPage'''# selenium控制爬取頁數(shù)count = 0page_count = 0while page_count < page_count_end:try:page_count += 1chrome.execute_script(js_code)time.sleep(0.3)except:continueimg_desc_search_urls = re.findall('href="(/search/detail\?.*?)"', chrome.page_source, re.S) # re.S使.可以匹配換行符# 獲取所有圖片的數(shù)據(jù)for img_data in img_desc_search_urls:try:# 獲取搜索圖片的參數(shù)os_ = re.findall('os=(.*?)&', img_data)[0]cs_ = re.findall('cs=(.*?)&', img_data)[0]if os_ and cs_:# 獲取搜索圖片的信息img_search_url = f'http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E9%A3%8E%E6%99%AF&step_word=&hs=0&pn=1&spn=0&di=195030&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs={cs_}&os={os_}'img_search_response = requests.get(img_search_url)img_search_data = img_search_response.text# 獲取圖片信息img_url = re.findall('''\('firstSc'\);" src="(.*?)"''', img_search_data)[0]img_name = img_url.split('/')[-1]img_name = os.path.join('百度圖片', img_name) # 拼接出圖片的地址,如 百度圖片/3822951_144045377000_2.jpg# 保存圖片img_response = requests.get(img_url)img_data = img_response.contentfw = open(img_name, 'wb')fw.write(img_data)fw.flush()# 提示count += 1print(f'{img_name}保存成功,成功保存{count}張')# 防止百度禁ip,慢一點(diǎn)time.sleep(0.01)except:continueexcept Exception:passfinally:chrome.close()## 以上代碼分兩部分,前部分加注釋的為數(shù)量30的百度圖片:后半部分未注釋的代碼,可以爬蟲百度圖片不限制,從谷歌打開,所以默認(rèn)瀏覽器是谷歌,另外根目錄要有個chromedriver.exe文件
轉(zhuǎn)載于:https://www.cnblogs.com/jinhongquan/p/11425674.html
總結(jié)
以上是生活随笔為你收集整理的爬虫百度图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。