简体   繁体   中英

How can I pass the result of a python script to the command line?

I have written a Python script that collects the magnet link of a torrent I want to download.

How can I then pass this to aria2c in the command line.

This is what I've tried so far, aria2c starts as if it will download but it won´t download a single byte. I am a complete noob so I apologise and thank you dearly for your help.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from pyYify import yify
import os
import subprocess
driver = webdriver.Chrome()

driver.get('https://mypiratebay.net/')
searchbox = driver.find_element_by_xpath('//*[@id="home"]/main/section/form/div[1]/input')
searchbox.send_keys('Toy Story 4')
searchbox.send_keys(Keys.RETURN)
torrentlink = driver.find_element_by_xpath('//*[@id="st"]/span[2]/a')
time.sleep(3)
torrentlink.click()

for a in driver.find_elements_by_xpath('//*[@id="d"]/a'):
    magnetlink = (a.get_attribute('href'))
    magnetlink = str(magnetlink)

subprocess.call(['aria2c', magnetlink])

To answer your asked question, at least partially:

You can grab a single output easily using the shell, although I am not sure how well that would work for your needs. On macos, using, bash:

test_181_cli.py:

import random
print (random.choice(["a","b","c"]))

test_181_cli.sh :

runmystuff(){
    url=$(python test_181_cli.py)
    printf "\nariac $url:"

    #what you actually want
    #ariac $url
}

So then you need to load your bash function definition:

source test_181_cli.sh (might be . test_181_cli.sh under Linux instead)

then to run it:

(venv38) myuser@explore$ runmystuff

ariac b:
(venv38) myuser@explore$ runmystuff

ariac c:

Now, as far as the ariac actually not working, I doubt that passing it to the command line would improve things upon subprocess and dealing with multiple urls would anyway be harder.

I suggest you print print(f":{magnetlink}:") Note the intentional colons - check if there are no extra blanks and format it appropriately. Then try running aria2c <your url> manually. You may have all sorts of other issues and subprocess, once you have a correct command line, is probably not the cause for them. Also, the remark by Daniel Walker is spot on: your subprocess call belongs in the loop.

You can try these steps:

  1. open a command prompt in the same folder

  2. type python file_name .

  3. the output of your program came up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM