1. 程式人生 > 實用技巧 >利用python爬取網頁圖片

利用python爬取網頁圖片

"""利用python爬取網頁圖片"""
import requests
import urllib
from bs4 import BeautifulSoup
import json

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
}
url = 'http://pic.sogou.com/pics/recommend?category=明星'
response = requests.get(url=url, headers=headers)
soup 
= BeautifulSoup(response.text, 'html.parser') print(soup.select('img')) def getSogouImg(category, length, path): url = 'https://pic.sogou.com/pics/channel/getAllRecomPicByTag.jsp?category=' + \ category + '&tag=全部&start=0&len=' + \ str(length) + '&width=1920&height=1080
' imgs = requests.get(url=url, headers=headers) jd = json.loads(imgs.text) jd = jd['all_items'] imgs_url = [] for j in jd: imgs_url.append(j['ori_pic_url']) m = 0 for img_url in imgs_url: print('------' + str(m) + '.jpg------' + ' Downloading...') urllib.request.urlretrieve(img_url, path
+ str(m) + '.jpg') m += 1 print('Download complete!') getSogouImg('桌布', 3000, 'd:/Download/桌布/')

轉載自:https://www.cnblogs.com/dearvee/category/966215.html