在 python 中合并 cd && curl

combine cd && curl in python

这是我用来从网站下载 pdf 的工具。 当我不组合 cd.... && 部分时,curl 启动并下载文件。 但是,每当我使用 cd 命令更改目录并下载文件时,它只会传递 curl 命令。 我不想为 curl 提供 -o 参数,因为我不愿意为文件提供自定义名称。 请提出此问题的原因和解决方案。

这个问题是独一无二的,因为它要求使用 bash 命令实现 curl。建议的线程仅与 bash 命令有关。

import subprocess
import shlex

url = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'
sessionID = input('Please, enter jsessionid...\n')
sessionID = str(sessionID) # Cookies
cookies_from_function = " -H 'Cookie: rppValue=20; B_View=1; JSESSIONID=" + sessionID + "'"
tempstring =  '-L -O -C - ' + url + " -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:64.0) Gecko/20100101 Firefox/64.0' -H 'Accept: */*' --compressed -H 'Connection: keep-alive'" + cookies_from_function# Login To Browser, inspect element, go to network tab, reload, copy curl url for a pdf link. Extract headers with cookies and paste here.
# print(tempstring)
curl_cmd = "cd /Volumes/path/to/destination/ && curl " + tempstring# Original
subprocess.call(shlex.split(curl_cmd))

&& 是一个 shell 逻辑运算符,用于 运行 如果前一个命令成功则执行命令。所以,你需要 运行 它里面 shell;使用 shell=True 并将其作为字符串而不是列表传递:

subprocess.call(curl_cmd, shell=True)

运行 直接在 shell 中命令,除非消毒可能会产生可想而知的灾难性影响。

附带说明一下,您应该查看使用 os 和一些网络客户端直接在 Python 中执行操作,例如requests.


另外,如果不想使用curl-o选项,可以使用shell重定向运算符(>)来保存curl 的 STDOUT 到某个文件:

curl -s ... >/out/file

-s 静音 curl 这样我们就不会在 STDERR 上获得进度状态。

正如评论中所建议的,您可以将 subprocess 函数的 cwd 关键字参数用于不同目录中的 运行。另一个简单的选择是 open 一个合适的文件并将其作为 stdout 传递给 subprocess 调用。

切线地,您可能想使用 check_call 或现代替代品 run 而不是非常基本的 call.

import subprocess
import os

url = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'
sessionID = input('Please, enter jsessionid...\n')
# No need, input aways returns a string in Python 3
# sessionID = str(sessionID) # Cookies
with open(os.path.join('/Volumes/path/to/destination', 'dummy.pdf')) as pdf:
    subprocess.check_call([
            'curl', '-L', '-C', '-', url,
            '-H', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:64.0) Gecko/20100101 Firefox/64.0',
            '-H', 'Accept: */*', '--compressed',
            '-H', 'Connection: keep-alive',
            '-H', 'Cookie: rppValue=20; B_View=1; JSESSIONID={0}'.format(sessionID)],
          stdout=pdf)

这也取消了 shlex,部分原因是您在评论中说您必须摆脱它,部分原因是它与将简单的静态命令行拆分为相比并没有真正提供任何重要价值手动标记一次(尽管您显然必须了解如何操作)。

如果您想保留 -O 选项,

subprocess.check_call([
    'curl', '-O', ...],
    cwd='/Volumes/path/to/destination')