繁体   English   中英

如何编写用于下载的python脚本?

[英]How to write a python script for downloading?

我想从这个站点下载一些文件: http : //www.emuparadise.me/soundtracks/highquality/index.php

但我只想得到某些。

有没有办法编写一个python脚本来做到这一点? 我有python的中级知识

我只是在寻找一些指导,请指点我去维基或图书馆来完成这个

谢谢,灌木

这是我的代码的链接

我看着页面。 这些链接似乎重定向到托管文件的另一个页面,单击该页面即可下载文件。

我会使用mechanize跟随所需的链接到正确的页面,然后使用BeautifulSouplxml来解析结果页面以获取文件名。

然后是使用urlopen打开文件并将其内容写入本地文件的简单问题,如下所示:

f = open(localFilePath, 'w')
f.write(urlopen(remoteFilePath).read())
f.close()

希望有帮助

为页面发出 url 请求。 获得源后,过滤并获取 url。

您要下载的文件是包含特定扩展名的 url。 正是有了这个,您可以对符合您的条件的所有网址进行正则表达式搜索。 过滤后,再对每个匹配到的url的数据做一个url请求,写入内存。

示例代码:

#!/usr/bin/python
import re
import sys
import urllib

#Your sample url
sampleUrl = "http://stackoverflow.com"
urlAddInfo = urllib.urlopen(sampleUrl)
data = urlAddInfo.read()

#Sample extensions we'll be looking for: pngs and pdfs
TARGET_EXTENSIONS = "(png|pdf)"
targetCompile = re.compile(TARGET_EXTENSIONS, re.UNICODE|re.MULTILINE)

#Let's get all the urls: match criteria{no spaces or " in a url}
urls = re.findall('(https?://[^\s"]+)', data, re.UNICODE|re.MULTILINE)

#We want these folks
extensionMatches = filter(lambda url: url and targetCompile.search(url), urls)

#The rest of the unmatched urls for which the scrapping can also be repeated.
nonExtMatches = filter(lambda url: url and not targetCompile.search(url), urls)


def fileDl(targetUrl):
  #Function to handle downloading of files.
  #Arg: url => a String
  #Output: Boolean to signify if file has been written to memory

  #Validation of the url assumed, for the sake of keeping the illustration short
  urlAddInfo = urllib.urlopen(targetUrl)
  data = urlAddInfo.read()
  fileNameSearch = re.search("([^\/\s]+)$", targetUrl) #Text right before the last slash '/'
  if not fileNameSearch:
     sys.stderr.write("Could not extract a filename from url '%s'\n"%(targetUrl))
     return False
  fileName = fileNameSearch.groups(1)[0]
  with open(fileName, "wb") as f:
    f.write(data)
    sys.stderr.write("Wrote %s to memory\n"%(fileName))
  return True

#Let's now download the matched files
dlResults = map(lambda fUrl: fileDl(fUrl), extensionMatches)
successfulDls = filter(lambda s: s, dlResults)
sys.stderr.write("Downloaded %d files from %s\n"%(len(successfulDls), sampleUrl))

#You can organize the above code into a function to repeat the process for each of the
#other urls and in that way you can make a crawler.

上面的代码主要是针对Python2.X编写的。 但是, 我写了一个爬虫,适用于从 2.X 开始的任何版本

为什么是! 5 年后,这不仅是可能的,而且您现在有很多方法可以做到。
我将避免在这里使用代码示例,因为主要是想帮助将您的问题分解为多个部分并为您提供一些探索选项:

第 1 部分:获取!

如果您必须坚持使用stdlib ,对于 python2 或 python3, urllib[n] *是您要用来从互联网上下拉某些东西的东西。
再说一次,如果您不想依赖其他软件包:

  • urlliburllib2或者另一个urllib[n]我忘记了。

如果您不必限制对标准库的导入:

你很幸运!!!!! 你有:

  • requests与文档在这里 requests是使用 python 从网络上获取东西的黄金标准。 我建议你使用它。
  • uplink与文档在这里 它相对较新,适用于更多程序化的客户端界面。
  • aiohttp通过asyncio与 docs here asyncio仅包含在 python >= 3.5 中,这也更加令人困惑。 也就是说,如果您愿意投入时间,那么对于这个用例来说,它的效率会高得离谱。

......我也有失职,更不用说我最喜欢的爬行工具之一:

  • fake_useragent回购在这里 文档之类的,真的没有必要。

第 2 部分:解析!

因此,如果您必须坚持使用 stdlib 并且不使用pip安装任何东西,您就可以使用额外的额外乐趣和安全 (<==extreme-sarcasm) xml内置模块。 具体来说,您可以使用:

  • xml.etree.ElementTree()与 docs here

值得注意的是ElementTree对象是 pip-downloadable lxml包的基础,并且使其更易于使用。 如果你想重新创建轮子并编写一堆你自己的复杂逻辑,使用默认的xml模块是你的选择。

如果您不必限制对标准库的导入:

  • 带有文档的lxml这里 正如我之前所说, lxmlxml.etree的包装器,它使它可供人类使用并实现您需要自己制作的所有解析工具。 但是,正如您通过访问文档所看到的,它本身并不容易使用。 这让我们...
  • BeautifulSoup又名bs4这里有文档。 BeautifulSoup 让一切变得更简单。 这是我的建议。

第 3 部分:获取获取获取!

这部分几乎与“Segment 1”完全相同,只是你有一堆链接而不是一个。 这部分和“段1”之间的变化是我用什么推荐的唯一的事: aiohttp这里有多个URL打交道时,因为它会下载方式快,您可以下载它们并行。 **


* - (其中n以某种令人沮丧的任意方式从 python-version 确定为 ptyhon-version。查找哪个urllib[n].urlopen()作为顶级函数。您可以阅读有关此命名的更多信息- 约定 clusterf**k此处此处此处。)
** - (这并不完全正确。在人类时间尺度上它在功能上更真实。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM