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

Python 爬取網(wǎng)頁圖片詳解流程

 更新時(shí)間:2021年11月09日 10:25:05   作者:不俠居  
沒想到python是如此強(qiáng)大,令人著迷,以前看見圖片總是一張一張復(fù)制粘貼,現(xiàn)在好了,學(xué)會(huì)python就可以用程序?qū)⒁粡垙垐D片,保存下來。今天網(wǎng)上沖浪看到很多美圖,可是圖片有點(diǎn)多,不想一張一張地復(fù)制粘貼,怎么辦呢?辦法總是有的,即便沒有我們也可以創(chuàng)造一個(gè)辦法

簡介

快樂在滿足中求,煩惱多從欲中來

記錄程序的點(diǎn)點(diǎn)滴滴。
輸入一個(gè)網(wǎng)址從這個(gè)網(wǎng)址中解析出圖片,并將它保存在本地

流程圖

在這里插入圖片描述

程序分析

解析主網(wǎng)址

def get_urls():
    url = 'http://www.nipic.com/show/35350678.html' # 主網(wǎng)址
    pattern = "(http.*?jpg)"
    header = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
    }
    r = requests.get(url,headers=header)
    r.encoding = r.apparent_encoding
    html = r.text
    urls = re.findall(pattern,html)
    return urls

url 為需要爬的主網(wǎng)址
pattern 為正則匹配
header 設(shè)置請(qǐng)求頭
r = requests.get(url,headers=header) 發(fā)送請(qǐng)求
r.encoding = r.apparent_encoding 設(shè)置編碼格式,防止出現(xiàn)亂碼

屬性 說明
r.encoding 從http header中提取響應(yīng)內(nèi)容編碼
r.apparent_encoding 從內(nèi)容中分析出的響應(yīng)內(nèi)容編碼

urls = re.findall(pattern,html) 進(jìn)行正則匹配
re.findall(pattern, string, flags=0)
正則 re.findall 的簡單用法(返回string中所有與pattern相匹配的全部字串,返回形式為數(shù)組)

下載圖片并存儲(chǔ)

def download(url_queue: queue.Queue()):
    while True:
        url = url_queue.get()
        root_path = 'F:\\1\\' # 圖片存放的文件夾位置
        file_path = root_path + url.split('/')[-1] #圖片存放的具體位置
        try:
            if not os.path.exists(root_path): # 判斷文件夾是是否存在,不存在則創(chuàng)建一個(gè)
                os.makedirs(root_path)
            if not os.path.exists(file_path): # 判斷文件是否已存在
                r = requests.get(url)
                with open(file_path,'wb') as f:
                    f.write(r.content)
                    f.close()
                    print('圖片保存成功')
            else:
                print('圖片已經(jīng)存在')
        except Exception as e:
            print(e)
        print('線程名: ', threading.current_thread().name,"url_queue.size=", url_queue.qsize())

此函數(shù)需要傳一個(gè)參數(shù)為隊(duì)列
queue模塊中提供了同步的、線程安全的隊(duì)列類,queue.Queue()為一種先入先出的數(shù)據(jù)類型,隊(duì)列實(shí)現(xiàn)了鎖原語,能夠在多線程中直接使用。可以使用隊(duì)列來實(shí)現(xiàn)線程間的同步。
url_queue: queue.Queue() 這是一個(gè)隊(duì)列類型的數(shù)據(jù),是用來存儲(chǔ)圖片URL
url = url_queue.get() 從隊(duì)列中取出一個(gè)url
url_queue.qsize() 返回隊(duì)形內(nèi)元素個(gè)數(shù)

全部代碼

import requests
import re
import os
import threading
import queue
def get_urls():
    url = 'http://www.nipic.com/show/35350678.html' # 主網(wǎng)址
    pattern = "(http.*?jpg)"
    header = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
    }
    r = requests.get(url,headers=header)
    r.encoding = r.apparent_encoding
    html = r.text
    urls = re.findall(pattern,html)
    return urls
def download(url_queue: queue.Queue()):
    while True:
        url = url_queue.get()
        root_path = 'F:\\1\\' # 圖片存放的文件夾位置
        file_path = root_path + url.split('/')[-1] #圖片存放的具體位置
        try:
            if not os.path.exists(root_path):
                os.makedirs(root_path)
            if not os.path.exists(file_path):
                r = requests.get(url)
                with open(file_path,'wb') as f:
                    f.write(r.content)
                    f.close()
                    print('圖片保存成功')
            else:
                print('圖片已經(jīng)存在')
        except Exception as e:
            print(e)
        print('線程名:', threading.current_thread().name,"圖片剩余:", url_queue.qsize())

if __name__ == "__main__":
	url_queue = queue.Queue()
	urls = tuple(get_urls())
	for i in urls:
	    url_queue.put(i)
	t1 = threading.Thread(target=download,args=(url_queue,),name="craw{}".format('1'))
	t2 = threading.Thread(target=download,args=(url_queue,),name="craw{}".format('2'))
	
	t1.start()
	t2.start()

到此這篇關(guān)于Python 爬取網(wǎng)頁圖片詳解流程的文章就介紹到這了,更多相關(guān)Python 爬取網(wǎng)頁圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python flask中靜態(tài)文件的管理方法

    python flask中靜態(tài)文件的管理方法

    下面小編就為大家分享一篇python flask中靜態(tài)文件的管理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 自定義PyCharm快捷鍵的設(shè)置方式

    自定義PyCharm快捷鍵的設(shè)置方式

    這篇文章主要介紹了自定義PyCharm快捷鍵的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 在PyCharm中三步完成PyPy解釋器的配置的方法

    在PyCharm中三步完成PyPy解釋器的配置的方法

    今天小編就為大家分享一篇在PyCharm中三步完成PyPy解釋器的配置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 詳解python之異步編程

    詳解python之異步編程

    這篇文章主要為大家介紹了python之異步編程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>
    2021-12-12
  • Pytorch固定隨機(jī)數(shù)種子的方法小結(jié)

    Pytorch固定隨機(jī)數(shù)種子的方法小結(jié)

    在對(duì)神經(jīng)網(wǎng)絡(luò)模型進(jìn)行訓(xùn)練時(shí),有時(shí)候會(huì)存在對(duì)訓(xùn)練過程進(jìn)行復(fù)現(xiàn)的需求,然而,每次運(yùn)行時(shí) Pytorch、Numpy 中的隨機(jī)性將使得該目的變得困難重重,基于此,本文記錄了 Pytorch 中的固定隨機(jī)數(shù)種子的方法,需要的朋友可以參考下
    2023-12-12
  • web.py獲取上傳文件名的正確方法

    web.py獲取上傳文件名的正確方法

    這篇文章主要介紹了web.py獲取上傳文件名的正確方法,遇到這個(gè)問題的朋友可能會(huì)困惑半天,使用本文的正確方法就可以解決這個(gè)問題了,需要的朋友可以參考下
    2014-08-08
  • Python浮點(diǎn)數(shù)四舍五入問題的分析與解決方法

    Python浮點(diǎn)數(shù)四舍五入問題的分析與解決方法

    這篇文章主要給大家介紹了關(guān)于Python中浮點(diǎn)數(shù)四舍五入問題的分析與解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 淺析python中5個(gè)帶key的內(nèi)置函數(shù)

    淺析python中5個(gè)帶key的內(nèi)置函數(shù)

    這篇文章主要介紹了python中5個(gè)帶key的內(nèi)置函數(shù),包括max取最大值函數(shù),min取最小值函數(shù),filter過濾函數(shù),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Python動(dòng)力系統(tǒng)驗(yàn)證三體人是否真的存在

    Python動(dòng)力系統(tǒng)驗(yàn)證三體人是否真的存在

    這篇文章主要介紹了Python動(dòng)力系統(tǒng)驗(yàn)證三體人是否真的存在,文中含有詳細(xì)的圖文示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • pandas將DataFrame的列變成行索引的方法

    pandas將DataFrame的列變成行索引的方法

    下面小編就為大家分享一篇pandas將DataFrame的列變成行索引的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04

最新評(píng)論