欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python自動(dòng)爬取圖片并保存實(shí)例代碼

 更新時(shí)間:2022年01月02日 09:30:40   作者:清忖灬  
大家好,本篇文章主要講的是Python自動(dòng)爬取圖片并保存實(shí)例代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下

一、準(zhǔn)備工作

用python來實(shí)現(xiàn)對(duì)百度圖片的爬取并保存,以情緒圖片為例,百度搜索可得到下圖所示

f12打開源碼

在此處可以看到這次我們要爬取的圖片的基本信息是在img - scr中

二、代碼實(shí)現(xiàn)

這次的爬取主要用了如下的第三方庫(kù)

import re
import time
import requests
from bs4 import BeautifulSoup
import os

簡(jiǎn)單構(gòu)思可以分為三個(gè)小部分

1.獲取網(wǎng)頁內(nèi)容

2.解析網(wǎng)頁

3.保存圖片至相應(yīng)位置

下面來看第一部分:獲取網(wǎng)頁內(nèi)容

baseurl = 'https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'
head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"}
    response = requests.get(baseurl, headers=head)  # 獲取網(wǎng)頁信息
    html = response.text  # 將網(wǎng)頁信息轉(zhuǎn)化為text形式

是不是so easy

第二部分解析網(wǎng)頁才是大頭

來看代碼

Img = re.compile(r'img.*src="(.*?)"')  # 正則表達(dá)式匹配圖片
soup = BeautifulSoup(html, "html.parser")  # BeautifulSoup解析html
    #i = 0  # 計(jì)數(shù)器初始值
    data = []  # 存儲(chǔ)圖片超鏈接的列表
    for item in soup.find_all('img', src=""):  # soup.find_all對(duì)網(wǎng)頁中的img—src進(jìn)行迭代
        item = str(item)  # 轉(zhuǎn)換為str類型
        Picture = re.findall(Img, item)  # 結(jié)合re正則表達(dá)式和BeautifulSoup, 僅返回超鏈接
        for b in Picture:
            data.append(b)
            #i = i + 1
            return data[-1]
 
    # print(i)

這里就運(yùn)用到了BeautifulSoup以及re正則表達(dá)式的相關(guān)知識(shí),需要有一定的基礎(chǔ)哦

下面就是第三部分:保存圖片

    for m in getdata(
            baseurl='https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'):
        resp = requests.get(m)  #獲取網(wǎng)頁信息
        byte = resp.content  # 轉(zhuǎn)化為content二進(jìn)制
        print(os.getcwd()) # os庫(kù)中輸出當(dāng)前的路徑
        i = i + 1 # 遞增
        # img_path = os.path.join(m)
        with open("path{}.jpg".format(i), "wb") as f: # 文件寫入
            f.write(byte)
            time.sleep(0.5) # 每隔0.5秒下載一張圖片放入D://情緒圖片測(cè)試
        print("第{}張圖片爬取成功!".format(i))

各行代碼的解釋已經(jīng)給大家寫在注釋中啦,不明白的地方可以直接私信或評(píng)論哦~

下面是完整的代碼

import re
import time
import requests
from bs4 import BeautifulSoup
import os
 
 
 
# m = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.uihwmxDdgfK4FlCIXx-3jgHaPc?w=115&h=183&c=7&r=0&o=5&pid=1.7'
'''
resp = requests.get(m)
byte = resp.content
print(os.getcwd())
img_path = os.path.join(m)
'''
def main():
    baseurl = 'https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'
    datalist = getdata(baseurl)
 
 
def getdata(baseurl):
    Img = re.compile(r'img.*src="(.*?)"')  # 正則表達(dá)式匹配圖片
    datalist = []
    head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"}
    response = requests.get(baseurl, headers=head)  # 獲取網(wǎng)頁信息
    html = response.text  # 將網(wǎng)頁信息轉(zhuǎn)化為text形式
    soup = BeautifulSoup(html, "html.parser")  # BeautifulSoup解析html
    # i = 0  # 計(jì)數(shù)器初始值
    data = []  # 存儲(chǔ)圖片超鏈接的列表
    for item in soup.find_all('img', src=""):  # soup.find_all對(duì)網(wǎng)頁中的img—src進(jìn)行迭代
        item = str(item)  # 轉(zhuǎn)換為str類型
        Picture = re.findall(Img, item)  # 結(jié)合re正則表達(dá)式和BeautifulSoup, 僅返回超鏈接
        for b in Picture:  # 遍歷列表,取最后一次結(jié)果
            data.append(b)
            # i = i + 1
            datalist.append(data[-1])
    return datalist  # 返回一個(gè)包含超鏈接的新列表
    # print(i)
 
'''
with open("img_path.jpg","wb") as f:
    f.write(byte)
'''
 
if __name__ == '__main__':
    os.chdir("D://情緒圖片測(cè)試")
 
    main()
    i = 0  # 圖片名遞增
    for m in getdata(
            baseurl='https://cn.bing.com/images/search?q=%E6%83%85%E7%BB%AA%E5%9B%BE%E7%89%87&qpvt=%e6%83%85%e7%bb%aa%e5%9b%be%e7%89%87&form=IGRE&first=1&cw=418&ch=652&tsc=ImageBasicHover'):
        resp = requests.get(m)  #獲取網(wǎng)頁信息
        byte = resp.content  # 轉(zhuǎn)化為content二進(jìn)制
        print(os.getcwd()) # os庫(kù)中輸出當(dāng)前的路徑
        i = i + 1 # 遞增
        # img_path = os.path.join(m)
        with open("path{}.jpg".format(i), "wb") as f: # 文件寫入
            f.write(byte)
            time.sleep(0.5) # 每隔0.5秒下載一張圖片放入D://情緒圖片測(cè)試
        print("第{}張圖片爬取成功!".format(i))

最后的運(yùn)行截圖

 三、總結(jié)

到此這篇關(guān)于Python自動(dòng)爬取圖片并保存實(shí)例代碼的文章就介紹到這了,更多相關(guān)Python爬取圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論