源代码:

    1. import requests
    2. from bs4 import BeautifulSoup
    3. import time
    4. if __name__=='__main__':
    5. #UA伪装:将对应的user-agent封装到一个字典中
    6. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    7. #1、指定url
    8. url='https://www.umeitu.com/meinvtupian/'
    9. #2、发起请求 请求对应的url是携带参数的,并且请求过程中处理了参数
    10. response = requests.get(url=url,headers=headers)
    11. response.encoding='utf-8'
    12. page=BeautifulSoup(response.text,"html.parser")
    13. # print(page)
    14. lst = page.find("div", class_='TypeList').find_all("a")
    15. for a in lst:
    16. href='https://www.umeitu.com'+a.get('href') # print(href) 此刻拿到子页面的源代码
    17. #拿到子页面的源代码
    18. child_page_resp=requests.get(href)#直接通过get拿到属性的值
    19. child_page_resp.encoding='utf-8'
    20. child_page_text=child_page_resp.text
    21. #从子页面中拿到图片的下载路径
    22. child_page=BeautifulSoup(child_page_text,"html.parser")#把源代码交给bs4
    23. p=child_page.find("div", class_="ImageBody")
    24. img=p.find("img")#找到img
    25. scr=img.get("src")#通过get拿到scr属性的值
    26. #下载图片
    27. img_resp=requests.get(scr)
    28. img_name=scr.split("/")[-1]
    29. with open("img/"+img_name,mode='wb') as f:
    30. f.write(img_resp.content)
    31. print("over!",img_name)
    32. time.sleep(1)
    33. print("all over!!")

    分析:

    访问网站,可以得知,我们是要先找到主页链接,找到herf下的链接,然后拼接,得到一个子链接,再访问,然后找到img,然后获取scr图片链接,然后保存下来
    image.png
    image.png
    1、先抓取页面源代码;

    1. #先爬取简单的网页html
    2. import requests
    3. from bs4 import BeautifulSoup
    4. if __name__=='__main__':
    5. #UA伪装:将对应的user-agent封装到一个字典中
    6. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    7. #1、指定url
    8. url='https://www.umeitu.com/'
    9. #2、发起请求 请求对应的url是携带参数的,并且请求过程中处理了参数
    10. response = requests.get(url=url,headers=headers)
    11. response.encoding='utf-8' #处理乱码
    12. print(response.text)

    image.png
    2、分析网页主页源代码,搜索TypeList可以发现有两处,第二处下面的数据不是我们想要的,而且还是TypeList2,所以可以用这个唯一属性

    来缩小范围
    image.png

    为什么不是主页面的图片链接? 这个是一个小图片,点击进去,是一个小图,我们要的是高清大图,所以要子链接里面的图片链接

    image.png
    image.png
    然后提取子页面的链接地址,先爬取所有的a标签
    page.find("div", class_=’TypeList’).find_all("a")

    1. import requests
    2. from bs4 import BeautifulSoup
    3. if __name__=='__main__':
    4. #UA伪装:将对应的user-agent封装到一个字典中
    5. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    6. #1、指定url
    7. url='https://www.umeitu.com/meinvtupian/'
    8. #2、发起请求 请求对应的url是携带参数的,并且请求过程中处理了参数
    9. response = requests.get(url=url,headers=headers)
    10. response.encoding='utf-8'#处理乱码
    11. page=BeautifulSoup(response.text,"html.parser")
    12. # print(page) #可以先打印这一步看看,一步步来
    13. lst = page.find("div", class_='TypeList').find_all("a") #先查找一个属性为<div class="TypeList">,find.all()查找所有的a标签
    14. print(lst)

    image.png
    3、拿到所有的子链接,通过href拿到子页面的内容

    1. import requests
    2. from bs4 import BeautifulSoup
    3. if __name__=='__main__':
    4. #UA伪装:将对应的user-agent封装到一个字典中
    5. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    6. #1、指定url
    7. url='https://www.umeitu.com/meinvtupian/'
    8. #2、发起请求 请求对应的url是携带参数的,并且请求过程中处理了参数
    9. response = requests.get(url=url,headers=headers)
    10. response.encoding='utf-8'#处理乱码
    11. page=BeautifulSoup(response.text,"html.parser")
    12. # print(page) #可以先打印这一步看看,一步步来
    13. lst = page.find("div", class_='TypeList').find_all("a")
    14. # print(lst)
    15. for a in lst:
    16. href='https://www.umeitu.com'+a.get('href')#通过拼接可以得到一个完整的子链接
    17. print(href) #此刻拿到子页面的源代码

    image.png
    4、访问子链接后,从子页面中找到图片的下载地址, img—->src
    img=p.find("img")#找到img
    img.get("src")

    image.png

    1. import requests
    2. from bs4 import BeautifulSoup
    3. import time
    4. if __name__=='__main__':
    5. #UA伪装:将对应的user-agent封装到一个字典中
    6. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    7. #1、指定url
    8. url='https://www.umeitu.com/meinvtupian/'
    9. #2、发起请求 请求对应的url是携带参数的,并且请求过程中处理了参数
    10. response = requests.get(url=url,headers=headers)
    11. response.encoding='utf-8'
    12. #把源代码交给bs4
    13. page=BeautifulSoup(response.text,"html.parser")
    14. # print(page)
    15. lst = page.find("div", class_='TypeList').find_all("a")
    16. for a in lst:
    17. href='https://www.umeitu.com'+a.get('href') # print(href) 此刻拿到子页面的源代码
    18. #拿到子页面的源代码
    19. child_page_resp=requests.get(href)#直接通过get拿到属性的值
    20. child_page_resp.encoding='utf-8'
    21. child_page_text=child_page_resp.text
    22. #从子页面中拿到图片的下载路径
    23. child_page=BeautifulSoup(child_page_text,"html.parser")#把源代码交给bs4
    24. p=child_page.find("div", class_="ImageBody")
    25. img=p.find("img")#找到img
    26. print(img.get("src"))#通过get拿到scr属性的值

    image.png
    5、下载图片

    1. import requests
    2. from bs4 import BeautifulSoup
    3. import time
    4. if __name__=='__main__':
    5. #UA伪装:将对应的user-agent封装到一个字典中
    6. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
    7. #1、指定url
    8. url='https://www.umeitu.com/meinvtupian/'
    9. #2、发起请求 请求对应的url是携带参数的,并且请求过程中处理了参数
    10. response = requests.get(url=url,headers=headers)
    11. response.encoding='utf-8'
    12. page=BeautifulSoup(response.text,"html.parser")
    13. # print(page)
    14. lst = page.find("div", class_='TypeList').find_all("a")
    15. for a in lst:
    16. href='https://www.umeitu.com'+a.get('href') # print(href) 此刻拿到子页面的源代码
    17. #拿到子页面的源代码
    18. child_page_resp=requests.get(href)#直接通过get拿到属性的值
    19. child_page_resp.encoding='utf-8'
    20. child_page_text=child_page_resp.text
    21. #从子页面中拿到图片的下载路径
    22. child_page=BeautifulSoup(child_page_text,"html.parser")#把源代码交给bs4
    23. p=child_page.find("div", class_="ImageBody")
    24. img=p.find("img")#找到img
    25. scr=img.get("src")#通过get拿到scr属性的值
    26. #下载图片
    27. img_resp=requests.get(scr)
    28. img_name=scr.split("/")[-1]
    29. with open("img/"+img_name,mode='wb') as f:
    30. f.write(img_resp.content)
    31. print("over!",img_name)
    32. time.sleep(1)
    33. print("all over!!")

    image.png
    6、爬取结束!