1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import requests from bs4 import BeautifulSoup
""" wallhaven.cc """ def requestWallHaven(page, root_dir): """ 壁纸数据爬取 :param page: 页数 :param root_dir: 图片存放地址 :return: """ res = requests.get("https://wallhaven.cc/hot?page=" + page).text soup = BeautifulSoup(res, "html.parser") for item in soup.find_all("a", "preview"): print("页面地址: %s" % item.get("href")) body = requests.get(item.get("href")).text body_soup = BeautifulSoup(body, "html.parser") img_url = body_soup.find("img", id="wallpaper").get("src") print("图片地址: %s" % img_url) data = img_url.split("/") file_name = data[len(data) - 1] print("文件名称: %s " % file_name) with open(root_dir + file_name, "wb") as f: f.write(requests.get(img_url).content) print("Page => %s Success!" % page)
if __name__ == '__main__': for i in range(32, 130): requestWallHaven(str(i), "E:\\Wallhaven\\") print("全部下载完毕!")
|