我在Python中使用urllib的urlretrieve()函数来尝试从网站获取一些pdf。它(至少对我而言)停止工作,正在下载损坏的数据(15 KB而不是164 KB)。Python:urlretrieve PDF下载
我用几个pdf测试过了,都没有成功(即random.pdf)。我似乎无法使其工作,并且我需要能够为我正在处理的项目下载pdf。
这里是我使用的下载PDF格式的(和分析使用pdftotext.exe文本)的那种代码的例子:我是新手程序员
def get_html(url): # gets html of page from Internet
import os
import urllib2
import urllib
from subprocess import call
f_name = url.split('/')[-2] # get file name (url must end with '/')
try:
if f_name.split('.')[-1] == 'pdf': # file type
urllib.urlretrieve(url, os.getcwd() + '\\' + f_name)
call([os.getcwd() + '\\pdftotext.exe', os.getcwd() + '\\' + f_name]) # use xpdf to output .txt file
return open(os.getcwd() + '\\' + f_name.split('.')[0] + '.txt').read()
else:
return urllib2.urlopen(url).read()
except:
print 'bad link: ' + url
return ""
,所以任何输入将是巨大的!谢谢
哇,这似乎很奇怪,谢谢你告诉我有关请求。 – hisroar