Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 抓取:从url下载文件_Python_R_Curl_Selenium_Web Scraping - Fatal编程技术网

Python 抓取:从url下载文件

Python 抓取:从url下载文件,python,r,curl,selenium,web-scraping,Python,R,Curl,Selenium,Web Scraping,我想从这个网站自动下载文件 我尝试了很多方法,比如: download.file read.table GET 但是没有成功。我不是要代码,而是要任何提示/想法来处理这种情况。使用的Python版本 这对我很有用: getIt = function(what,when){ url=paste0("http://oilandgas.ky.gov/Production%20Reports%20Library/", when,"%20-%20",what

我想从这个网站自动下载文件

我尝试了很多方法,比如:

download.file
read.table
GET
但是没有成功。我不是要代码,而是要任何提示/想法来处理这种情况。

使用的Python版本

这对我很有用:

getIt = function(what,when){ 
     url=paste0("http://oilandgas.ky.gov/Production%20Reports%20Library/",
                 when,"%20-%20",what,
                "%20Production.xls")
     destfile=paste0("/tmp/",what,when,".xls")
     download.file(url,destfile)
}
例如:

> getIt("gas",2006)
trying URL 'http://oilandgas.ky.gov/Production%20Reports%20Library/2006%20-%20gas%20Production.xls'
Content type 'application/vnd.ms-excel' length 3490304 bytes (3.3 Mb)
opened URL
==================================================
downloaded 3.3 Mb
除了第一个:

> getIt("oil",2010)
trying URL 'http://oilandgas.ky.gov/Production%20Reports%20Library/2010%20-%20oil%20Production.xls'
Error in download.file(url, destfile) : 
  cannot open URL 'http://oilandgas.ky.gov/Production%20Reports%20Library/2010%20-%20oil%20Production.xls'
In addition: Warning message:
In download.file(url, destfile) :
  cannot open: HTTP status was '404 NOT FOUND'
虽然我可以得到2010年的天然气数据:

> getIt("gas",2010)
trying URL 'http://oilandgas.ky.gov/Production%20Reports%20Library/2010%20-%20gas%20Production.xls'
Content type 'application/vnd.ms-excel' length 4177408 bytes (4.0 Mb)
opened URL
==================================================
downloaded 4.0 Mb
所以看起来他们改变了这个链接的系统。您可以通过以下方式获取该数据:单击该链接,然后在粗糙的Sharepoint HTML中查找下载链接


这就是为什么我们讨厌Sharepoint,孩子们。

对于Python,一种常见的方法是使用why not download.file?对我有用。@Spacedman你能给我看看这个吗?也许我错过了什么?我们的工作不是让你知道我们做了什么。你的工作就是向我们展示你的尝试和失败。你没有这样做。请用更多细节编辑您的问题。@Spacedman您是对的(通常我会展示我所尝试的,也许您已经知道了)。我没有个人电脑(现在),一旦我得到它,我会编辑我的问题。+1我也讨厌它,但它无处不在:)你可以看到我的尝试,它甚至可以与你的第一个文件。很抱歉,之前没有显示我的尝试(技术原因)。Argh他们第一次改为使用xlsx而不是xls。感谢您提供此解决方案
Beautiful组
非常漂亮。
> getIt("gas",2010)
trying URL 'http://oilandgas.ky.gov/Production%20Reports%20Library/2010%20-%20gas%20Production.xls'
Content type 'application/vnd.ms-excel' length 4177408 bytes (4.0 Mb)
opened URL
==================================================
downloaded 4.0 Mb