-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetpic.py
More file actions
19 lines (19 loc) · 743 Bytes
/
getpic.py
File metadata and controls
19 lines (19 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import requests
from bs4 import BeautifulSoup
# 图片地址
dest_url = r'https://colorhub.me/search?tag=%E4%B8%AD%E5%9B%BD'
r = requests.get(dest_url)
soup = BeautifulSoup(r.text,'html.parser')
# 链接
hrefs = [i.find('a')['href'] for i in soup.find_all('div',class_='card')];
# 标题
titles = [i.find('a')['title'] for i in soup.find_all('div',class_='card')];
for href,title in zip(hrefs,titles):
res = requests.get(href)
soup_res = BeautifulSoup(res.text,'html.parser')
src = soup_res.find('img',class_="card-img-top")['src']
pic_response=requests.get('https:'+src)
# 下载保存图片
with open(title + '.jpg', mode='wb') as fn:
print('正在下载',title+'.jpg')
fn.write(pic_response.content)