我想運行python test.py "https://google.com"
或python test.py https://google.com
代替 ASKING 作為輸入...作為 python 的新手,我還沒有成功地做到這一點。我怎么能這樣做?
我的 python 代碼如下所示:我試過 def main(url) 但沒有用。我也嘗試在 main 下使用行程,但也沒有運氣!
import re
import urllib
def main():
import core.downloader as downloader
import core.pdf as pdf
print("Starting...\n")
url = input("Enter the url of the PDF:")
# Check that the URL provided by the user points to the entire document
# and not to a specific page (e.g. https://issuu.com/user/docs/doc
# instead of https://issuu.com/user/docs/doc/18)
url_end = re.search(r'(. )/\d /?$', url)
if url_end:
# If there is a page number at the end of the URL
print('The URL provided points to a specific page in the document.')
url_without_page_number = url_end.group(1)
print('Using the following URL instead:')
print(url_without_page_number)
url = url_without_page_number
else:
# If the URL points to the entire document, without any page number
pass
url_open1 = str(urllib.request.urlopen(url).read().decode("utf-8"))
# Credits to https://txt2re.com/ for the regex (Almost all of it)
# Sorry, I'm not lazy, but I hate making regex's
re1 = '.*?'
re2 = '((?:http|https)(?::\\/{2}[\\w] )(?:[\\/|\\.]?)(?:[^\\s"]*)(?:png|jpg))'
rg = re.compile(re1 re2, re.IGNORECASE | re.DOTALL)
m = rg.search(url_open1)
if m:
httpurl = m.group(1)
print('Starting from URI: ' httpurl)
filelist = downloader.downloader(httpurl)
pdf.creator(filelist)
else:
print("Error! No image was found")
if __name__ == '__main__':
main()
uj5u.com熱心網友回復:
如果要從用戶那里獲取引數,可以匯入sys庫,例如:
import sys
first_file_name = sys.argv[1]
second_file_name = sys.argv[2]
請注意索引從 1 開始,因為第一個引數是您運行的檔案的名稱。如果您想了解更多關于 sys 和 argv 的資訊: 如何在 Python 中使用 sys.argv
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489810.html
標籤:python-3.x 网址
上一篇:嘗試在django模板html檔案中反轉url時,出現例外“NoReverseMatch”。我在視圖函式中包含了附加引數
