爬虫入门(三)

前面我们找到网站的url和伪装浏览器之后就可以写代码了

res = requests.get(add,headers=headers)
soup = BeautifulSoup(res.text,'lxml')
img_list = soup.find_all('img',attrs={'class':'lazy image_dtb img-responsive'})#class 中是下载路径

def download_img(url):
    split_list = url.split('/')#图片名分割
    filename = split_list.pop()
    path = os.path.join("images")#图片的路径
    # 判断是否有这个文件夹没有的话创建文件夹
    if not os.path.exists(path):
        os.mkdir(path)
    else:
        mypath=os.path.join("images",filename)
        request.urlretrieve(url,filename=mypath)

for img in img_list:
    img_really = img['data-original']
    download_img(img_really)

  

猜你喜欢

转载自www.cnblogs.com/fsrmyc/p/10025510.html
今日推荐