首页 > 其他 > 详细

爬虫入门(三)

时间:2018-11-27 12:15:01      阅读:148      评论:0      收藏:0      [点我收藏+]

前面我们找到网站的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)

  

爬虫入门(三)

原文:https://www.cnblogs.com/fsrmyc/p/10025510.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!