working on a python project, and what it does is it looks at the index of lifehacker.com, then finds all tags with the class "headline h5 hover-highlight entry-title", then it creates files for each directory. But the only problem is that when i run it, i get OSError: [Errno 2] No such file or directory: "/home/root/python/The Sony Smartwatch 3: A Runner's Perspective (Updated: 1/5/2015)"
help would be lovely, thanks!
heres my code atm:
import re
import os
import urllib2
from bs4 import BeautifulSoup
from mechanize import Browser
url = "http://lifehacker.com/"
url_open = urllib2.urlopen(url)
soup = BeautifulSoup(url_open.read())
link = soup.findAll("h1",{"class": "headline h5 hover-highlight entry-title"})
file_directory = "/home/root/python/"
for i in link:
os.mkdir(os.path.join(file_directory, str(i.text)))
print "Successfully made directory(s)", i.text, "!"
else:
print "The directory", i.text, "either exists, or there was an error!"
Sanitize your filename. (Failing to do so is also going to cause you security issues, particularly if you don't prevent things from starting with ../
).
This could be as simple as:
safe_name = i.text.replace('/', '_')
os.mkdir(os.path.join(file_directory, safe_name))
As it is, your code is trying to create a directory named 2015)
, in a directory named 5
, in a directory named The Sony Smartwatch 3: A Runner's Perspective (Updated: 1
. Since none of those exist, and os.mkdir()
isn't recursive, you get the error in question. (If you want a recursive operation, see os.makedirs()
instead).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With