使用 Python 抓取和下載 Google 圖像
已發表: 2023-01-02Web 抓取的日益普及大大增加了它的使用範圍。 如今,許多人工智能應用程序定期向網絡抓取數據集提供最新數據。 從目標網站到人工智能應用程序的數據集,數據有規律且不間斷地流動。
相關文章:人工智能對客戶成功未來的影響
圖像處理是人工智能應用中最熱門的領域之一。 圖像處理是計算機科學的一個領域,專注於使計算機能夠識別和理解圖像和視頻中的物體和人物。 與其他類型的人工智能一樣,圖像處理旨在執行和自動化複製人類能力的任務。 在這種情況下,圖像處理試圖同時復制人們的觀看方式和他們理解所見內容的方式。
在許多領域,尤其是在圖像處理項目中,算法開發和減少誤差所需的數據都是通過網絡抓取獲得的。 在本文中,我們將開發一個在圖像處理項目中經常使用的應用程序。 我們將使用 Python 編程語言抓取和下載 Google Images。 讓我們開始吧。
項目設置
首先,讓我們在桌面上打開一個文件夾。 讓我們在此文件路徑中打開一個終端,並通過運行以下命令安裝必要的庫。
pip 安裝請求 bs4 |
安裝必要的庫後,讓我們在文件夾中創建一個名為“index.py”的文件。
代碼
讓我們將以下代碼粘貼到我們創建的“index.py”文件中。
導入請求、re、json、urllib.request 從 bs4 導入 BeautifulSoupheaders = { “User-Agent”:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36” }queryParameters = {“q”:“marvel”,“tbm”:“isch”,“hl”:“en”,“gl”:“us”,“ijn”:“0”} target_image_path = “.isv-r.PNCib.MSM1fd.BUooTd” html = requests.get(“https://www.google.com/search”, params=queryParameters, headers=headers, timeout=30) google_images = [] def scrape_and_download_google_images(): images_data_in_json = convert_image_to_json() matched_image_data = re.findall(r'\”b-GRID_STATE0\”(.*)sideChannel:\s?{}}', images_data_in_json) removed_matched_thumbnails = remove_matched_get_thumbnails(matched_google_image_data=matched_image_data) matched_resolution_images = re.findall(r”(?:'|,),\[\”(https:|http.*?)\”,\d+,\d+\]”, removed_matched_thumbnails) full_resolution_images = get_resolution_image(匹配分辨率圖像=匹配分辨率圖像) 對於枚舉(zip(soup.select(target_image_path),full_resolution_images),start = 1)中的索引,(image_data,image_link): append_image_to_list(圖像數據=圖像數據,圖像鏈接=圖像鏈接) print(f'{index}.圖片開始下載') 下載圖片(圖片鏈接=圖片鏈接,索引=索引) print(f'{index}. 圖片下載成功') 打印(f'抓取和下載的圖像:{google_images}') def remove_matched_get_thumbnails(matched_google_image_data): def get_resolution_image(matched_resolution_images): def convert_image_to_json(): def append_image_to_list(圖像數據,圖像鏈接): def download_image(圖像鏈接,索引): scrape_and_download_google_images() |
如果我們檢查代碼,讓我們首先看一下具有靜態值的字段。 以下字段定義為靜態值。 我們使用 queryParams 變量指定要抓取和下載的圖像的名稱和屬性。
另請閱讀:ChatGPT – 對企業的影響
標題= { “User-Agent”:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36” }queryParameters = {“q”:“marvel”,“tbm”:“isch”,“hl”:“en”,“gl”:“us”,“ijn”:“0”}target_image_path =“.isv- r.PNCib.MSM1fd.BUooTd” targeted_image_base_html_path = “.VFACy.kGQAp.sMi44c.lNHeqe.WGvvNb” html = requests.get(“https://www.google.com/search”, params=queryParameters, headers=headers, timeout=30) google_images = [] |
scrape_and_download_google_images()方法是流開始的地方。 目標圖像被抓取,然後下載到我們指定的文件夾中。
def scrape_and_download_google_images(): images_data_in_json = convert_image_to_json() matched_image_data = re.findall(r'\”b-GRID_STATE0\”(.*)sideChannel:\s?{}}', images_data_in_json) removed_matched_thumbnails = remove_matched_get_thumbnails(matched_google_image_data=matched_image_data) matched_resolution_images = re.findall(r”(?:'|,),\[\”(https:|http.*?)\”,\d+,\d+\]”, removed_matched_thumbnails) full_resolution_images = get_resolution_image(匹配分辨率圖像=匹配分辨率圖像) 對於枚舉(zip(soup.select(target_image_path),full_resolution_images),start = 1)中的索引,(image_data,image_link): append_image_to_list(圖像數據=圖像數據,圖像鏈接=圖像鏈接) print(f'{index}.圖片開始下載') 下載圖片(圖片鏈接=圖片鏈接,索引=索引) print(f'{index}. 圖片下載成功') 打印(f'抓取和下載的圖像:{google_images}') |
將抓取的圖像下載到文件夾是在 download_image(image_link=image_link, index=index) 方法中完成的。 用這種方法抓取的圖像保存在我們之前添加到項目文件位置的“Scraped_Images”中。
注意:在運行應用程序之前,在項目結構中創建“Scraped_Images”文件夾
def download_image(圖像鏈接,索引): opener=urllib.request.build_opener() opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36')] urllib.request.install_opener(開瓶器) urllib.request.urlretrieve(image_link, f'Scraped_Images/Image_{index}.jpg') |
另請閱讀:區塊鏈即服務對企業的應用和好處是什麼?
跑步
要運行該應用程序,讓我們在文件位置打開一個終端並運行以下命令。
蟒蛇指數.py |
應用運行後,應用的控制台打印如下信息。
1.鏡像開始下載 1. 圖片下載成功 2. 圖片開始下載 2. 圖片下載成功 3.鏡像開始下載 3. 圖片下載成功 4.圖片開始下載 4. 圖片下載成功 5.圖片開始下載 5.圖片下載成功 6.圖片開始下載 6.圖片下載成功 7. 圖片開始下載 7. 圖片下載成功 […] 48.圖片開始下載 48. image successfully downloaded抓取和下載的圖片: [ { 'image_title': '漫威漫畫 – 維基百科', 'image_source_link': 'https://en.wikipedia.org/wiki/Marvel_Comics', 'image_link':'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b9/Marvel_Logo.svg/1200px-Marvel_Logo.svg.png' }, { 'image_title': '漫威宇宙 – 維基百科', 'image_source_link': 'https://en.wikipedia.org/wiki/Marvel_Universe', 'image_link': 'https://upload.wikimedia.org/wikipedia/en/1/19/Marvel_Universe_%28Civil_War%29.jpg' }, { 'image_title':'Marvel.com | 漫威電影、角色、漫畫、電視的官方網站', 'image_source_link': 'https://www.marvel.com/', 'image_link': 'https://i.annihil.us/u/prod/marvel/images/OpenGraph-TW-1200×630.jpg' }, { 'image_title': '漫威娛樂 - YouTube', 'image_source_link': 'https://www.youtube.com/c/marvel', 'image_link': 'https://yt3.ggpht.com/fGvQjp1vAT1R4bAKTFLaSbdsfdYFDwAzVjeRVQeikH22bvHWsGULZdwIkpZXktcXZc5gFJuA3w=s900-ck-c0x00ffffff-no-rj' }, { 'image_title': '漫威電影和節目 | 迪士尼+', 'image_source_link': 'https://www.disneyplus.com/brand/marvel', 'image_link':'https://prod-ripcut-delivery.disney-plus.net/v1/variant/disney/DA2E198288BFCA56AB53340211B38DE7134E40E4521EDCAFE6FFB8CD69250DE9/scale?width=2880&aspectRatio=1.78&format=jpeg' }, { 'image_title': 'Marvel 新手入門 | 有線', 'image_source_link': 'https://www.wired.com/2012/03/an-intro-to-marvel-for-newbies/', 'image_link':'https://media.wired.com/photos/5955ceabcbd9b77a41915cf6/master/pass/marvel-characters.jpg' }, { 'image_title': '如何按故事順序觀看每部漫威電影 - 遊行:娛樂、食譜、健康、生活、假期', 'image_source_link': 'https://parade.com/1009863/alexandra-hurtado/marvel-movies-order/', 'image_link': 'https://parade.com/.image/t_share/MTkwNTgxMjkxNjk3NDQ4ODI4/marveldisney.jpg' }, { 'image_title': '漫威漫畫 | 歷史、人物、事實和電影 | 大英百科全書', 'image_source_link': 'https://www.britannica.com/topic/Marvel-Comics', 'image_link': 'https://cdn.britannica.com/62/182362-050-BD31B42D/Scarlett-Johansson-Black-Widow-Chris-Hemsworth-Thor.jpg' }, { 'image_title': '驚奇隊長 (2019) – IMDb', 'image_source_link': 'https://www.imdb.com/title/tt4154664/', 'image_link':'https://m.media-amazon.com/images/M/MV5BMTE0YWFmOTMtYTU2ZS00ZTIxLWE3OTEtYTNiYzBkZjViZThiXkEyXkFqcGdeQXVyODMzMzQ4OTI@._V1_FMjpg_UX1000_.jpg' }, […] ] |
讓我們看看“Scraped_Images”文件來檢查下載的圖像。
另請閱讀:公司類型以及如何整合您的初創企業
結論
我們使用 python 抓取和下載了出於某種原因需要的 Google 圖像。 如果您想在不編寫任何代碼的情況下從 Google 獲取所需的圖像,請探索 Zenserp API。 這是它不斷更新的強大而精彩的文檔。