爬蟲-圖像網站下載

爬蟲-圖像網站下載

@(Python)[python]

圖像網站下載

編寫一個程序,訪問圖像共享網站,如FlickrImgur,查找一個類型的照片,然後下載所有查詢結果的圖像。可以編寫一個程序,訪問任何具有查找功能的圖像網站。

import os
import requests
import bs4

baseUrl = 'https://imgur.com'
dirName = 'image'
os.makedirs(dirName, exist_ok=True)
# 搜索參數
url = baseUrl + '/search/score?q=' + 'movie'
response = requests.get(url)
response.raise_for_status()
soup = bs4.BeautifulSoup(response.text, "lxml")
imageUrls = soup.select(".image-list-link img")
if not imageUrls:
    print('Could not find image.')
else:
    for imageUrl in imageUrls:
        downloadUrl = imageUrl.get('src')
        print("Download image %s..." % downloadUrl)
        split = downloadUrl.split('/')
        fileName = os.path.basename(split[len(split) - 1])
        filePath = os.path.join(dirName, fileName)
        print("FilePath is  %s..." % filePath)
        if not os.path.exists(filePath):
            imageStream = requests.get('https:' + downloadUrl)
            imageStream.raise_for_status()
            imageFile = open(filePath, 'wb')
            for chunk in imageStream.iter_content(100000):
                imageFile.write(chunk)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章