本项目主要用到的库
本项目练习目的
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
import time
import os
import shutil
def load():
url = "https://www.meitulu.com/t/nvshen/"
html = get_url(url)
soup = BeautifulSoup(html,'html.parser')
href = soup.select('ul.img li p.p_title a')
for i in href:
get_pics(i['href'])
def get_pics(url):
html = get_url(url)
soup = BeautifulSoup(html,'html.parser')
pics = soup.select('div.content center img')
for i in pics:
pic = requests.get(i['src'])
with open('pictures\{}.jpg'.format(i['alt']),'wb')as f:
print('正在下载:{}'.format(i['alt']))
f.write(pic.content)
time.sleep(1)
def get_url(url):
html = requests.get(url)
html.encoding = 'utf-8'
html = html.text
return html
def make_dir():
path = 'pictures'
if(os.path.exists(path)):
shutil.rmtree(path)
os.mkdir(path)
def main():
make_dir()
load()
main()