&引用;“文件不存在”;使用Python 2.7中的请求动态创建PDF下载文件时

&引用;“文件不存在”;使用Python 2.7中的请求动态创建PDF下载文件时,python,file-io,beautifulsoup,python-requests,Python,File Io,Beautifulsoup,Python Requests,我正在尝试从网站动态下载pdf。我确信我正确地列出了它们,但我不确定我是否正确地执行了实际的文件I/O。我得到以下错误 File "download.py", line 22, in <module> with open("'"+url+"'", "wb") as pdf: IOError: [Errno 2] No such file or directory: "'http://www.lcs.mit.edu/publications/pubs/pdf/MIT-LCS

我正在尝试从网站动态下载pdf。我确信我正确地列出了它们,但我不确定我是否正确地执行了实际的文件I/O。我得到以下错误

  File "download.py", line 22, in <module>
    with open("'"+url+"'", "wb") as pdf:
IOError: [Errno 2] No such file or directory: "'http://www.lcs.mit.edu/publications/pubs/pdf/MIT-LCS-TR-179.pdf'"

如果
url
设置为
'http://www.lcs.mit.edu/publications/pubs/pdf/MIT-LCS-TR-179.pdf“
,您的代码失败,因为它试图在您的文件系统上打开具有该名称的文件

相反,请尝试以下方法:

fileForUrl = '/tmp/' + url.split('/')[-1]
with open(fileForUrl, 'wb') as pdf:
   # Rest of the code as before
fileForUrl = '/tmp/' + url.split('/')[-1]
with open(fileForUrl, 'wb') as pdf:
   # Rest of the code as before