抓取唯美图库(BeautifulSoup)

使用BeautifulSoup

 1、拿到主页面的源代码,然后提取到子页面的简介地址,href
 2、通过href拿到子页面的内容。从子页面中找到导图片的下载地址 img -> src
 3、下载图片 



import requests
from bs4 import BeautifulSoup

url='https://www.umei.cc/bizhitupian/weimeibizhi/'
resp = requests.get(url)
resp.encoding='utf8'  # 处理乱码
# print(resp.text)

main_page = BeautifulSoup(resp.text,"html.parser")
alist = main_page.find('div',class_="Clbc_r_cont").find_all("img", class_="box_div_yj")
# print(alist)
for a in alist:
    src = a.get('src')  # 可以通过get就可以拿到属性的值
    # print(src)
    # 下载图片
    img_resp = requests.get(src)
    # img_resp.content # 这里拿到的是字节
    img_name = src.split("/")[-1]  # 拿到url中的最后一个/以后的内容
    with open("img/"+img_name,mode='wb') as f:
        f.write(img_resp.content)  # 图片内容写入文件

    print("ovwe!!!",img_name)
print("all over!!!")

抓取唯美图库(BeautifulSoup)_第1张图片

 

你可能感兴趣的:(网络爬虫,beautifulsoup,网络爬虫,python)