nar*_*ary 5 python wget python-2.7
我用来os.system('wget '+ link)从网站检索文件。下载后,我想根据源链接进一步处理这些文件。
大多数链接都是这种形式\n htttp://example.com/.../filename.zip。
\n在这种情况下,文件只需下载为filename.zip. basename我可以使用RegExp 和 RegExp从链接中提取此内容[^/]+$。\n
\n但问题是表单的链接
http://http://www.ez-robot.com\nhttp://www.worldscientific.com/\nhttp://www.fairweld.com\nRun Code Online (Sandbox Code Playgroud)\n\n这些链接下载为index.html、index.html.1、index.html.2ans 等。
\n在这里,我无法区分哪个index文件属于哪个网站。我可以做到这一点的一种方法是查看链接传递到 的顺序wget。
我想要一些通用方法来获取在计算机中下载文件的“真实”文件名。执行完成后wget,终端显示a Saving to:,它会在终端上显示一个标签,后跟“真实”文件名。我想将该文件名存储在字符串中。
是否存在任何直接/更简单的方法来获取文件名?我正在使用Python。
\n\n$ wget http://www.fairweld.com\n--2015-04-11 18:51:48-- http://www.fairweld.com/\nConnecting to 202.142.81.24:3124... connected.\nProxy request sent, awaiting response... 200 OK\nLength: 39979 (39K) [text/html]\nSaving to: \xe2\x80\x98index.html.4\nRun Code Online (Sandbox Code Playgroud)\n
使用 os.path.basename 并根据 url 的结尾方式获取名称,您还可以使用 requests 来下载 html:
links = ["http://www.ez-robot.com",
"http://www.worldscientific.com/",
"http://www.fairweld.com"]
import urlparse
import requests
import os
for link in links:
r = requests.get(link)
if link.rsrip("/").endswith(".com"):
name = os.path.basename(link)
else:
name = urlparse.urlsplit(link.path.split("/")[-1])
with open("{}.html".format(name),"w") as f:
f.write(r.content)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3564 次 |
| 最近记录: |