安装
pip install bs4
代码
import requests
from bs4 import BeautifulSoup
import time
url = 'https://umei.cc/bizhitupian/weimeibizhi/'
domain = 'https://umei.cc'
resp = requests.get(url)
resp.encoding = 'utf-8'
# print(resp.text)
# 把网页源代码传给bs
page = BeautifulSoup(resp.text, 'html.parser')
a_list = page.find('div', class_='TypeList').find_all('a')
# print(a_list)
for a in a_list:
# 使用get可以直接拿到属性值
href = domain + a.get('href')
child_resp = requests.get(href)
child_resp.encoding = 'utf-8'
child_content = child_resp.text
# 从子页面中拿图片的下载路径
child_page = BeautifulSoup(child_content, 'html.parser')
p = child_page.find('p', align='center')
# print(p)
img = p.find('img')
src = img.get('src')
# 下载图片