DAN*_*OME 11 python beautifulsoup
在python项目上工作,它的作用是查看lifehacker.com的索引,然后查找所有带有"headline h5 hover-highlight entry-title"类的标签,然后为每个目录创建文件.但唯一的问题是,当我运行它,我得到OSError: [Errno 2] No such file or directory: "/home/root/python/The Sony Smartwatch 3: A Runner's Perspective (Updated: 1/5/2015)"
帮助会很可爱,谢谢!
继承我的代码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!"
Run Code Online (Sandbox Code Playgroud)
清理文件名。(如果不这样做,还会导致安全问题,尤其是在您不阻止开始时,尤其如此../)。
这可能很简单:
safe_name = i.text.replace('/', '_')
os.mkdir(os.path.join(file_directory, safe_name))
Run Code Online (Sandbox Code Playgroud)
照原样,您的代码正在尝试在名为目录的目录中创建2015)名为5的目录The Sony Smartwatch 3: A Runner's Perspective (Updated: 1。由于这些都不存在,并且os.mkdir()也不是递归的,因此您会遇到问题。(如果要进行递归操作,请参见os.makedirs())。
| 归档时间: |
|
| 查看次数: |
17536 次 |
| 最近记录: |