##来源半个码农
#1.准备工作,首先,我们需要安装必要的库,确保你已经安装了 requests 和 beautifulsoup4,
#如果没有可以通过以下命令进行安装:
pip install requests
pip install beautifulsoup4
#2.爬取壁纸
#我们选择了一个壁纸网站 https://www.umei.cc/bizhitupian/weimeibizhi/,通过该网站的页面结构,#我们可以轻松地爬取到美女图片的下载链接。里面有美女图片,请自行修改地址
import requests
from bs4 import BeautifulSoup
import time
url = "https://www.umei.cc/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = 'utf-8' # 处理乱码
# 把返回的内容交给bs
main_page = BeautifulSoup(resp.text, "html.parser")
alist = main_page.find("div", class_="Clbc_Game_l_a").find_all("a")
for a in alist:
href = url + a.get('href').split("/")[-1] # 直接通过get就可以拿到属性的值 ,拼接图片路径,拿到源代码
child_page_resp = requests.get(href) # 请求子页面
child_page_resp.encoding = 'utf-8