I can not figure out how to create a folder with the date today, and save screenshots there. And the next day, create another folder with tomorrow's date and write other files there, etc.

date_today = str (datetime.datetime.today (). strftime ("% d_% m_% Y_% H_% M")) name_file = str (n) + ' ' + ('sait ') + str (link_now) + str (date_today)

driver.save_screenshotg ('\' + date_today + '\' + name_file)

from selenium import webdriver from selenium.webdriver.common.keys import Keys import time,random,re,datetime,os,errno import traceback;#uotput error from selenium.webdriver.common.action_chains import ActionChains saity=['http://lenta.ru/rss/news','http://www.kommersant.ru/RSS/main.xml','http://www.kommersant.ru/RSS/news.xml','http://rssportal.ru/feed/173256.xml','http://www.levada.ru/rss.xml','http://wsrss.bbc.co.uk/russian/index.xml','http://inosmi.ru/misc/export/xml/rss/translation.xml','http://news.google.ru/news?ned=ru_ru&topic=t&output=atom','http://112.ua/rss','http://thebestvideo.net/feed','http://feeds.feedburner.com/Astrobene'] driver = webdriver.Chrome() driver.maximize_window() wait_1=9 n=0 for i in reversed(range(len(saity))): try: link_now = saity.pop(random.randint(0, i)) driver.get(link_now) time.sleep(wait_1) print(link_now) date_today= str(datetime.datetime.today().strftime("%d_%m_%Y_%H_%M")) name_file=str(n)+'_'+('sait_')+str(link_now)+str(date_today) name_file= name_file.replace('http://','').replace('.','').replace('/','_') name_file= name_file+'.png' #было бы замечательно в jpg print(name_file) driver.save_screenshot('\\'+date_today+'\\'+name_file) n+=1 except Exception as exc: print(exc) traceback.print_exc() 
  • one
    Something you have here all piled up in a bunch. the list is called saity , and the data you take from generatory . The variable n not defined. As for saving a file, it would not be bad to first create a folder into which you saved to assemble (for example, os.mkdir ). Concerning jpg - allselenium.info/… - strawdog
  • one
    1) strftime and so returns a string, so you do not need to call str for it 2) instead of name_file=str(n)+'_'+('sait_')+str(link_now)+str(date_today) write through ''.format or f-строки , the code will be easier, the world is kinder, etc. :) 3) when in the driver.save_screenshot transfer the path with a new folder, does it create a folder and save a screenshot there or swear on the path? - gil9red

1 answer 1

I can not figure out how to create a folder with the date today, and save screenshots there. And the next day, create another folder with tomorrow's date and write other files there, etc.

Look at the format table , it says that you will get hours and minutes in a row:

 date_today = datetime.datetime.today().strftime("%d_%m_%Y_%H_%M") 

Accordingly, in order to have only the current date of the day, you need to leave only the necessary:

 date_today = datetime.datetime.today().strftime("%d_%m_%Y") 

For example, for today's date, the date_today will be the string '24_12_2018'


Ps.

Instead

 name_file=str(n)+'_'+('sait_')+str(link_now)+str(date_today) 

try better:

 name_file = '{}_sait_{}{}'.format(n, link_now, date_today) 

or:

 name_file = f'{n}_sait_{link_now}{date_today}'