[英]create folder in python
How can I make this script grab the "nmv-fas" from the link name and create a directory with that name then place all the files that are downloaded in that directory. 如何使此脚本从链接名称中获取“ nmv-fas”并创建具有该名称的目录,然后将所有下载的文件放在该目录中。
all.html: all.html:
<a href="http://www.youversion.com/bible/gen.45.nmv-fas">http://www.youversion.com/bible/gen.45.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.46.nmv-fas">http://www.youversion.com/bible/gen.46.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.47.nmv-fas">http://www.youversion.com/bible/gen.47.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.48.nmv-fas">http://www.youversion.com/bible/gen.48.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.49.nmv-fas">http://www.youversion.com/bible/gen.49.nmv-fas</a>
<a href="http://www.youversion.com/bible/gen.50.nmv-fas">http://www.youversion.com/bible/gen.50.nmv-fas</a>
<a href="http://www.youversion.com/bible/exod.1.nmv-fas">http://www.youversion.com/bible/exod.1.nmv-fas</a>
<a href="http://www.youversion.com/bible/exod.2.nmv-fas">http://www.youversion.com/bible/exod.2.nmv-fas</a>
<a href="http://www.youversion.com/bible/exod.3.nmv-fas">http://www.youversion.com/bible/exod.3.nmv-fas</a>
files saved in folder named: 保存在以下文件夹中的文件:
nmv-fas
python: 蟒蛇:
import lxml.html as html
import urllib
import urlparse
from BeautifulSoup import BeautifulSoup
import re
root = html.parse(open('all.html'))
for link in root.findall('//a'):
url = link.get('href')
name = urlparse.urlparse(url).path.split('/')[-1]
f = urllib.urlopen(url)
s = f.read()
f.close()
soup = BeautifulSoup(s)
articleTag = soup.html.body.article
converted = str(articleTag)
open(name, 'w').write(converted)
You could use the lxml
module to parse links out of the file, and then use urllib
to download each link. 您可以使用lxml
模块从文件中解析出链接,然后使用urllib
下载每个链接。 Reading the links might look like this: 阅读链接可能如下所示:
import lxml.html as html
root = html.parse(open('links.html'))
for link in root.findall('//a'):
url = link.get('href')
You can download a link to a file using urllib.urlopen
: 您可以使用urllib.urlopen
下载指向文件的链接:
import urllib
import urlparse
# extract the final path component and use it as
# the local filename.
name = urlparse.urlparse(url).path.split('/')[-1]
fd = urllib.urlopen(url)
open(name, 'w').write(fd.read())
Put these together and you should have something similar to what you want. 将它们放在一起,您应该拥有与所需物品相似的物品。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.