6

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

但我只想得到某些。

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

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

谢谢,灌木

这是我的代码的链接

4

4 回答 4

4

我看了看页面。链接似乎重定向到托管文件的另一个页面,单击哪个页面下载文件。

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

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

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

希望有帮助

于 2012-09-25T22:34:34.900 回答
1

为页面发出 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 开始的任何版本

于 2013-07-31T16:27:10.717 回答
0

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

第 1 部分:获取!

如果您必须坚持使用stdlib,对于 python2 或 python3,urllib[n]*是您想要用来从 Internet 上下拉内容的东西。
同样,如果您不想依赖其他包:

  • urllib或者urllib2也许另一个urllib[n]我忘记了。

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

你很幸运!!!!你有:

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

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

  • fake_useragent回购这里。文档喜欢严重没有必要。

第 2 部分:解析!

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

  • xml.etree.ElementTree()这里有文档。

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

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

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

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

本节与“第 1 节”几乎完全相同,只是您有一堆链接而不是一个。本节和“Segment 1”之间唯一不同的是我对使用什么的建议:在处理多个 URL 时,aiohttp这里的下载速度会更快,因为它允许您并行下载它们。**


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

于 2018-07-28T20:27:28.593 回答
-1

我会使用 wget 的组合进行下载 - http://www.thegeekstuff.com/2009/09/the-ultimate-wget-download-guide-with-15-awesome-examples/#more-1885 和 BeautifulSoup http: //www.crummy.com/software/BeautifulSoup/bs4/doc/用于解析下载的文件

于 2012-09-25T22:40:09.383 回答