python爬虫(爬取蜂鸟网图片)_创建文件夹

来源:互联网 发布:python 图像分割 编辑:程序博客网 时间:2024/04/28 07:51
import requests,urllib,osfrom bs4 import BeautifulSoupans = 1for page in range(58,69):    url = 'http://bbs.fengniao.com/forum/pic/slide_101_6478020_768937' + str(page) + '.html'    header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36'}    source_code = requests.get(url,headers = header)    plain_text = source_code.text    soup = BeautifulSoup(plain_text,'lxml')    download_link = []    file_name = ''    for pic_tag in soup.find_all('a'):                #get the name        if pic_tag.get('href')=='/forum/6478020.html':            file_name = pic_tag.get('title')                if pic_tag.get('class')==['pictureDownload']:            download_link.append(pic_tag.get('href'))    #file setting    folder_path = "D:/spider_things/2016.4.6/" + file_name +"/"    if not os.path.exists(folder_path):       os.makedirs(folder_path)    for item in download_link:        urllib.urlretrieve(item,folder_path + str(ans) + '.jpg')        print 'download_',ans,'_pics'        ans=ans+1


这次爬虫应用了创建文件夹的功能:

    #file setting    folder_path = "D:/spider_things/2016.4.6/" + file_name +"/"    if not os.path.exists(folder_path):       os.makedirs(folder_path)
上面代码块的意思是:
"os.path.exists(folder_path)"用来判断folder_path这个路径是否存在,如果不存在,就执行“os.makedirs(folder_path)”来创建这个路径

0 0
原创粉丝点击