![](/img/trans.png)
[英]How can I create new directory and save file using urllib2 in Python?
[英]How to get latest file using “urllib2” by reading html directory in python
我想从http文件夹中读取最新文件
'releases'文件夹应该像0001.tgz,0002.tgz,0003.tgz如何制作0003将被选中?
import urllib2
url = "http://example.com/releases"
html = urllib2.urlopen(url).read()
...
谢谢。 给我一些例子。
您可以使用BeautifulSoup
或lxml
来解析目录索引,并根据您的命名约定查找最新文件,该文件可能是索引中的最后一个文件。
像这样的东西:
from bs4 import BeautifulSoup
import urllib2
url = "http://example.com/releases"
html = urllib2.urlopen(url).read()
soup = BeautifulSoup(html)
last_link = soup.find_all('a', href=True)[-1]
latest_content = urllib2.urlopen(last_link['href']).read()
# do stuff
如果这不起作用,请使用find_all
获取所有链接,并根据文件名进行更仔细的解析。
如果.tgz文件是顺序的,那么从最大值开始倒计时,当你到达第一个(最新的)文件时停止循环。
import urllib2
for counter in xrange(9999,0,-1):
fyle = str(counter).zfill(4) # pad zeros
url = "http://example.com/releases/"+fyle+".tgz"
ret = urllib2.urlopen(url)
if ret.code == 200:
print "Exists:",fyle
break
html = urllib2.urlopen(url).read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.