Tôi đang nhận được:
Couldn't find a tree builder with the features you requested: parser.html. Do you need to install a parser library?
Khi nào tôi cố gắng phân tích trang của mình. Xin vui lòng giúp đỡ vì tôi vẫn không giỏi lắm về Python.
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
Hỏi ngày 8 tháng 8 năm 2019 lúc 12:56Aug 8, 2019 at 12:56
0
Trình phân tích cú pháp phải là "html.parser" chứ không phải "trình phân tích cú pháp.html"
Tham khảo tài liệu súp đẹp để biết chi tiết.
Đã trả lời ngày 8 tháng 8 năm 2019 lúc 13:01Aug 8, 2019 at 13:01
Koushikmlnkoushikmlnkoushikmln
5564 Huy hiệu bạc21 Huy hiệu đồng4 silver badges21 bronze badges
1
Sử dụng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
03 thay vì import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
04. Điều này có thể hoạt động.Nếu không, hãy thử:
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
05Đã trả lời ngày 8 tháng 8 năm 2019 lúc 13:00Aug 8, 2019 at 13:00
Anthony Ranthony rAnthony R
2.5291 Huy hiệu vàng11 Huy hiệu bạc10 Huy hiệu đồng1 gold badge11 silver badges10 bronze badges
Mới trong phiên bản 3.2.
Mã nguồn: lib/argparse.py Lib/argparse.py
Mô-đun
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 giúp dễ dàng viết các giao diện dòng lệnh thân thiện với người dùng. Chương trình xác định những đối số mà nó yêu cầu và import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 sẽ tìm ra cách phân tích các đối số ra khỏi import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
08. Mô -đun import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 cũng tự động tạo thông báo trợ giúp và sử dụng. Mô -đun cũng sẽ phát hành lỗi khi người dùng đưa ra các đối số không hợp lệ.Chức năng cốt lõi¶
Mô-đun
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 hỗ trợ cho các giao diện dòng lệnh được xây dựng xung quanh một thể hiện là import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
11. Nó là một thùng chứa cho các thông số kỹ thuật đối số và có các tùy chọn áp dụng toàn bộ trình phân tích cú pháp:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
Phương pháp
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
12 gắn các thông số kỹ thuật đối số riêng lẻ vào trình phân tích cú pháp. Nó hỗ trợ các đối số vị trí, các tùy chọn chấp nhận các giá trị và cờ bật/tắt:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
Phương thức
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
13 chạy trình phân tích cú pháp và đặt dữ liệu được trích xuất vào đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
14:args = parser.parse_args[] print[args.filename, args.count, args.verbose]
Liên kết nhanh cho add_argument []
hoạt động | Chỉ định cách xử lý một đối số | 15, 16, 17, 18, 19, 20, 21, 22
|
sự lựa chọn | Giới hạn các giá trị đối với một tập hợp các lựa chọn cụ thể | 23, 24 hoặc 25
|
const | Lưu trữ một giá trị không đổi | |
default | Giá trị mặc định được sử dụng khi không được cung cấp đối số | Mặc định là 26
|
Dest | Chỉ định tên thuộc tính được sử dụng trong không gian tên kết quả | |
Cứu giúp | Trợ giúp thông điệp cho một đối số | |
Metavar | Tên hiển thị thay thế cho đối số như được hiển thị trong trợ giúp | |
nargs | Số lần đối số có thể được sử dụng | 27, 28, 29, 30 hoặc 31
|
yêu cầu | Cho biết liệu một đối số là bắt buộc hay tùy chọn | 32 hoặc 33
|
loại hình | Tự động chuyển đổi một đối số thành loại đã cho | 27, 35, 36 hoặc chức năng có thể gọi được
|
Thí dụ¶
Mã sau đây là một chương trình Python lấy một danh sách các số nguyên và tạo ra tổng hoặc tối đa:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
Giả sử mã Python ở trên được lưu vào một tệp có tên
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
37, nó có thể được chạy ở dòng lệnh và nó cung cấp các thông báo trợ giúp hữu ích:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]
Khi chạy với các đối số thích hợp, nó in tổng hoặc tối đa của số nguyên dòng lệnh:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 10
Nếu các đối số không hợp lệ được truyền vào, một lỗi sẽ được hiển thị:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'
Các phần sau đây hướng dẫn bạn qua ví dụ này.
Tạo trình phân tích cú pháp
Bước đầu tiên trong việc sử dụng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 là tạo đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39:>>> parser = argparse.ArgumentParser[description='Process some integers.']
Đối tượng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 sẽ chứa tất cả thông tin cần thiết để phân tích dòng lệnh vào các loại dữ liệu Python.Thêm đối số
Việc điền vào
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 với thông tin về các đối số chương trình được thực hiện bằng cách thực hiện các cuộc gọi đến phương thức import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42. Nói chung, các cuộc gọi này nói với import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Cách lấy các chuỗi trên dòng lệnh và biến chúng thành các đối tượng. Thông tin này được lưu trữ và sử dụng khi import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 được gọi. Ví dụ:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
0Sau đó, gọi
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 sẽ trả về một đối tượng với hai thuộc tính, import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
46 và import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
47. Thuộc tính import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
46 sẽ là danh sách một hoặc nhiều số nguyên và thuộc tính import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
47 sẽ là hàm import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
50, nếu import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
51 được chỉ định tại dòng lệnh hoặc hàm import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
52 nếu không.Phân tích đối số
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Đối số phân tích cú pháp thông qua phương thức import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44. Điều này sẽ kiểm tra dòng lệnh, chuyển đổi từng đối số thành loại thích hợp và sau đó gọi hành động thích hợp. Trong hầu hết các trường hợp, điều này có nghĩa là một đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
55 đơn giản sẽ được xây dựng từ các thuộc tính được phân tích từ dòng lệnh:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
1Trong một tập lệnh,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 thường sẽ được gọi mà không có đối số và import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 sẽ tự động xác định các đối số dòng lệnh từ import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
08.Đối tượng ArbutionParser
classArgParse.ArgumentParser [prog = none, use = none, description = none, epilog = none, cha mẹ = [], formatter_class = argparse.helpformatter, prefix_chars = '-', fromfile_prefix_chars , add_help = true, allow_abbrev = true, exit_on_error = true] ¶ argparse.ArgumentParser[prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True]¶Tạo một đối tượng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 mới. Tất cả các tham số nên được truyền làm đối số từ khóa. Mỗi tham số có mô tả chi tiết hơn dưới đây, nhưng tóm lại chúng là:Prog - Tên của chương trình [mặc định:
60]import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Cách sử dụng - Chuỗi mô tả việc sử dụng chương trình [mặc định: được tạo từ các đối số được thêm vào trình phân tích cú pháp]
Mô tả - Văn bản để hiển thị trước khi trợ giúp đối số [theo mặc định, không có văn bản]
Epilog - văn bản để hiển thị sau khi trợ giúp đối số [theo mặc định, không có văn bản]
Cha mẹ - Danh sách các đối tượng
39 có đối số cũng nên được đưa vàoimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
formatter_class - một lớp để tùy chỉnh đầu ra trợ giúp
prefix_chars - tập hợp các ký tự có tiền tố các đối số tùy chọn [mặc định: ‘ -‘]
FromFile_Prefix_Chars - tập hợp các ký tự tiền tố mà từ đó các đối số bổ sung sẽ được đọc [mặc định:
26]import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
argars_default - Giá trị mặc định toàn cầu cho các đối số [mặc định:
26]import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
xung đột_handler - Chiến lược giải quyết các tùy chọn xung đột [thường là không cần thiết]
ADD_HELP - Thêm tùy chọn
64 vào trình phân tích cú pháp [mặc định:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
32]import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
allow_abbrev - cho phép các tùy chọn dài được viết tắt nếu viết tắt là không rõ ràng. [Mặc định:
32]import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
EXIT_ON_ERROR - Xác định xem ArgumentParser có thoát ra với thông tin lỗi hay không khi xảy ra lỗi. [Mặc định:
32]import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Đã thay đổi trong phiên bản 3.5: Tham số allow_abbrev đã được thêm vào.allow_abbrev parameter was added.
Đã thay đổi trong phiên bản 3.8: Trong các phiên bản trước, allow_abbrev cũng vô hiệu hóa nhóm các cờ ngắn như
68 có nghĩa là import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
69.In previous versions, allow_abbrev also disabled grouping of short flags such as import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
68 to mean import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
69.Đã thay đổi trong phiên bản 3.9: tham số EXIT_ON_ERROR đã được thêm vào.exit_on_error parameter was added.
Các phần sau đây mô tả cách mỗi trong số này được sử dụng.
ăn xin¶
Theo mặc định, các đối tượng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 sử dụng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
71 để xác định cách hiển thị tên của chương trình trong các thông báo trợ giúp. Mặc định này hầu như luôn luôn mong muốn vì nó sẽ làm cho các thông báo trợ giúp khớp với cách chương trình được gọi trên dòng lệnh. Ví dụ: hãy xem xét một tệp có tên import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
72 với mã sau:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
2Sự trợ giúp cho chương trình này sẽ hiển thị
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
72 dưới dạng tên chương trình [bất kể chương trình được gọi từ đâu]:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
3Để thay đổi hành vi mặc định này, một giá trị khác có thể được cung cấp bằng đối số
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
74 thành import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
4Lưu ý rằng tên chương trình, cho dù được xác định từ
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
71 hoặc từ đối số import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
74, có sẵn để trợ giúp tin nhắn bằng trình xác định định dạng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
78.import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
5cách sử dụng¶
Theo mặc định,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Tính toán thông báo sử dụng từ các đối số mà nó chứa:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
6Thông báo mặc định có thể được ghi đè với đối số từ khóa
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
80:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
7Trình xác định định dạng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
78 có sẵn để điền tên chương trình trong các thông báo sử dụng của bạn.sự mô tả¶
Hầu hết các cuộc gọi đến trình xây dựng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 sẽ sử dụng đối số từ khóa import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
83. Lập luận này đưa ra một mô tả ngắn gọn về những gì chương trình làm và cách thức hoạt động. Trong các thông báo trợ giúp, mô tả được hiển thị giữa chuỗi sử dụng dòng lệnh và các thông báo trợ giúp cho các đối số khác nhau:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
8Theo mặc định, mô tả sẽ được bọc dòng để nó phù hợp với không gian đã cho. Để thay đổi hành vi này, hãy xem đối số Formatter_Class.
Epilogn
Một số chương trình muốn hiển thị mô tả bổ sung của chương trình sau khi mô tả các đối số. Văn bản như vậy có thể được chỉ định bằng đối số
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
84 thành import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
9Như với đối số mô tả, văn bản
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
84 được bao bọc theo dòng mặc định, nhưng hành vi này có thể được điều chỉnh bằng đối số formatter_ class thành import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39.cha mẹ¶
Đôi khi, một số trình phân tích cú pháp chia sẻ một bộ đối số chung. Thay vì lặp lại các định nghĩa của các đối số này, một trình phân tích cú pháp duy nhất với tất cả các đối số được chia sẻ và có thể sử dụng đối số
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
88 cho import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39. Đối số import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
88 lấy một danh sách các đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39, thu thập tất cả các hành động vị trí và tùy chọn từ chúng và thêm các hành động này vào đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 được xây dựng:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']0
Lưu ý rằng hầu hết các trình phân tích cú pháp cha sẽ chỉ định
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
93. Mặt khác, import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 sẽ thấy hai tùy chọn import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
64 [một trong cha mẹ và một trong trẻ] và gây ra lỗi.Ghi chú
Bạn phải khởi tạo hoàn toàn các trình phân tích cú pháp trước khi chuyển chúng qua
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
88. Nếu bạn thay đổi trình phân tích cú pháp cha mẹ sau khi trình phân tích cú pháp con, những thay đổi đó sẽ không được phản ánh ở trẻ.
formatter_ class¶¶
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Các đối tượng cho phép định dạng trợ giúp được tùy chỉnh bằng cách chỉ định một lớp định dạng thay thế. Hiện tại, có bốn lớp như vậy:
classargparse.rawdescriphelpformatterr classargparse.rawtexthelpformatter¶ classargparse.argumentdefaultshelpformatterrargparse.RawDescriptionHelpFormatter¶ class
argparse.RawTextHelpFormatter¶ class
argparse.ArgumentDefaultsHelpFormatter¶ class
argparse.MetavarTypeHelpFormatter¶import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
98 và import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
99 Cung cấp nhiều quyền kiểm soát hơn về cách mô tả văn bản được hiển thị. Theo mặc định, import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Đối tượng đã bao bọc dòng mô tả và văn bản Epilog trong các thông báo trợ giúp dòng lệnh:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']1
Vượt qua
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
98 như parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']02 chỉ ra rằng mô tả và epilog đã được định dạng chính xác và không nên được bọc dòng:
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']2
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
99 duy trì khoảng trắng cho tất cả các loại văn bản trợ giúp, bao gồm các mô tả đối số. Tuy nhiên, nhiều dòng mới được thay thế bằng một. Nếu bạn muốn bảo quản nhiều dòng trống, hãy thêm khoảng trống giữa các dòng mới.parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']04 Tự động thêm thông tin về các giá trị mặc định vào từng thông báo trợ giúp đối số:
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']3
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']05 sử dụng tên của đối số loại cho mỗi đối số làm tên hiển thị cho các giá trị của nó [thay vì sử dụng định mệnh như định dạng thông thường]:
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']4
prefix_chars¶
Hầu hết các tùy chọn dòng lệnh sẽ sử dụng
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 làm tiền tố, ví dụ:
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']07. Các trình phân tích cú pháp cần hỗ trợ các ký tự tiền tố khác nhau hoặc bổ sung, ví dụ: Đối với các tùy chọn như
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']08 hoặc
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']09, có thể chỉ định chúng bằng đối số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 cho Trình xây dựng ArmparParser:
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']5
Đối số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 mặc định là
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']12. Việc cung cấp một tập hợp các ký tự không bao gồm
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 sẽ khiến các tùy chọn
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']07 không được phép.
Fromfile_prefix_chars¶
Đôi khi, khi xử lý một danh sách đối số đặc biệt dài, có thể có ý nghĩa để giữ danh sách các đối số trong một tệp thay vì gõ nó vào dòng lệnh. Nếu đối số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']15 được đưa ra cho hàm tạo
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39, thì các đối số bắt đầu với bất kỳ ký tự được chỉ định nào sẽ được coi là tệp và sẽ được thay thế bằng các đối số mà chúng chứa. Ví dụ:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']6
Các đối số được đọc từ một tệp theo mặc định là một dòng trên mỗi dòng [nhưng xem thêm
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']17] và được xử lý như thể chúng ở cùng một nơi với đối số tham chiếu tệp gốc trên dòng lệnh. Vì vậy, trong ví dụ trên, biểu thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']18 được coi là tương đương với biểu thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']19.
Đối số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']15 mặc định là
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
26, có nghĩa là các đối số sẽ không bao giờ được coi là tham chiếu tệp.arging_default¶
Nói chung, các mặc định đối số được chỉ định bằng cách chuyển một mặc định cho
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 hoặc bằng cách gọi các phương thức parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']23 với một tập hợp các cặp giá trị tên cụ thể. Tuy nhiên, đôi khi, có thể hữu ích khi chỉ định một mặc định toàn trình phân tích cú pháp cho các đối số. Điều này có thể được thực hiện bằng cách chuyển đối số từ khóa
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']24 cho
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39. Ví dụ: để triệt tiêu toàn cầu tạo thuộc tính trên các cuộc gọi import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44, chúng tôi cung cấp parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']27:
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']7
cho phép_abbrev¶
Thông thường, khi bạn chuyển một danh sách đối số cho phương thức
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 của import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39, nó sẽ nhận ra chữ viết tắt của các tùy chọn dài.recognizes abbreviations of long options.Tính năng này có thể bị tắt bằng cách đặt
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']30 thành
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
33:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']8
Mới trong phiên bản 3.5.
xung đột_handler¶
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Đối tượng không cho phép hai hành động có cùng chuỗi tùy chọn. Theo mặc định, các đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 nêu ra một ngoại lệ nếu một nỗ lực được thực hiện để tạo đối số với một chuỗi tùy chọn đã được sử dụng:parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']9
Đôi khi [ví dụ: khi sử dụng cha mẹ], có thể hữu ích khi chỉ cần ghi đè bất kỳ đối số cũ nào với cùng một chuỗi tùy chọn. Để có được hành vi này, giá trị
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']34 có thể được cung cấp cho đối số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']35 của
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag0
Lưu ý rằng các đối tượng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 chỉ xóa một hành động nếu tất cả các chuỗi tùy chọn của nó bị ghi đè. Vì vậy, trong ví dụ trên, hành động parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']07 cũ được giữ lại dưới dạng hành động
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']39, bởi vì chỉ chuỗi tùy chọn
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']40 đã được ghi đè.
add_help¶
Theo mặc định, các đối tượng ArgentParSer thêm một tùy chọn chỉ hiển thị thông báo trợ giúp của trình phân tích cú pháp. Ví dụ: hãy xem xét một tệp có tên
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
72 chứa mã sau:import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
2Nếu
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']42 hoặc
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']43 được cung cấp tại dòng lệnh, Trợ giúp của ArbutionParser sẽ được in:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag2
Đôi khi, nó có thể hữu ích để vô hiệu hóa việc bổ sung tùy chọn trợ giúp này. Điều này có thể đạt được bằng cách chuyển
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
33 dưới dạng đối số parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']45 cho
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag3
Tùy chọn trợ giúp thường là
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
64. Ngoại lệ cho điều này là nếu parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']10 được chỉ định và không bao gồm
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06, trong trường hợp đó
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']42 và
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']43 không phải là các tùy chọn hợp lệ. Trong trường hợp này, ký tự đầu tiên trong
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']52 được sử dụng để tiền tố các tùy chọn trợ giúp:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag4
exit_on_error¶
Thông thường, khi bạn chuyển một danh sách đối số không hợp lệ cho phương thức
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 của import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39, nó sẽ thoát với thông tin lỗi.Nếu người dùng muốn bắt lỗi theo cách thủ công, tính năng này có thể được bật bằng cách đặt
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']55 thành
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
33:parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag5
Mới trong phiên bản 3.9.
Phương thức add_argument []
ArcharchParser.add_argument [nameorflags ... [, hành động] [, nargs] [, const] [, mặc định] [, loại] [, lựa chọn] [, yêu cầu] [, trợ giúp]add_argument[name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest]]¶Xác định cách một đối số dòng lệnh duy nhất nên được phân tích cú pháp. Mỗi tham số có mô tả chi tiết hơn dưới đây, nhưng tóm lại chúng là:
Tên hoặc cờ - tên hoặc danh sách các chuỗi tùy chọn, ví dụ:
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
57 hoặcparser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
58.Hành động - Loại hành động cơ bản sẽ được thực hiện khi gặp đối số này ở dòng lệnh.
NARGS - Số lượng các đối số dòng lệnh nên được tiêu thụ.
Const - một giá trị không đổi được yêu cầu bởi một số lựa chọn hành động và NARGS.
Mặc định - giá trị được tạo ra nếu đối số không có trong dòng lệnh và nếu nó không có trong đối tượng không gian tên.
Loại - Loại mà đối số dòng lệnh phải được chuyển đổi.
Lựa chọn - Một thùng chứa của các giá trị cho phép cho đối số.
Yêu cầu - có hay không tùy chọn dòng lệnh có thể được bỏ qua hay không [chỉ có tùy chọn].
Trợ giúp - Một mô tả ngắn gọn về những gì đối số làm.
Metavar - Tên cho đối số trong các thông báo sử dụng.
DEST - Tên của thuộc tính sẽ được thêm vào đối tượng được trả về bởi
44.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Các phần sau đây mô tả cách mỗi trong số này được sử dụng.
Tên hoặc Cờ
Phương pháp
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 phải biết liệu một đối số tùy chọn, như parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']39 hay
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']40 hay đối số vị trí, như danh sách các tên tệp, được mong đợi. Do đó, các đối số đầu tiên được chuyển đến
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 phải là một loạt các cờ hoặc một tên đối số đơn giản.Ví dụ, một đối số tùy chọn có thể được tạo như:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag6
Trong khi một đối số vị trí có thể được tạo như:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag7
Khi
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 được gọi, các đối số tùy chọn sẽ được xác định bởi tiền tố parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 và các đối số còn lại sẽ được coi là vị trí:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag8
hoạt động¶
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Đối tượng liên kết các đối số dòng lệnh với các hành động. Những hành động này có thể thực hiện bất cứ điều gì với các đối số dòng lệnh được liên kết với chúng, mặc dù hầu hết các hành động chỉ đơn giản là thêm một thuộc tính vào đối tượng được trả về bởi import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44. Đối số từ khóa parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']68 Chỉ định cách xử lý các đối số dòng lệnh. Các hành động được cung cấp là:
15 - Điều này chỉ lưu trữ giá trị đối số. Đây là hành động mặc định. Ví dụ:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
9
16 - Điều này lưu trữ giá trị được chỉ định bởi đối số từ khóa Const; Lưu ý rằng đối số từ khóa Const mặc định làimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
26. Hành độngimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
16 được sử dụng phổ biến nhất với các đối số tùy chọn chỉ định một số loại cờ. Ví dụ:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
0
17 vàimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
74 - Đây là những trường hợp đặc biệt của
16 được sử dụng để lưu trữ các giá trịimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
32 vàimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
33 tương ứng. Ngoài ra, chúng tạo ra các giá trị mặc định lần lượt làimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
33 vàimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
32. Ví dụ:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
1
18 - Điều này lưu trữ một danh sách và nối mỗi giá trị đối số vào danh sách. Nó rất hữu ích để cho phép một tùy chọn được chỉ định nhiều lần. Nếu giá trị mặc định là không trống, các phần tử mặc định sẽ có trong giá trị phân tích cú pháp cho tùy chọn, với bất kỳ giá trị nào từ dòng lệnh được thêm vào sau các giá trị mặc định đó. Ví dụ sử dụng:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
2
19 - Điều này lưu trữ một danh sách và nối các giá trị được chỉ định bởi đối số từ khóa Const vào danh sách; Lưu ý rằng đối số từ khóa Const mặc định làimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
26. Hành độngimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
19 thường hữu ích khi nhiều đối số cần lưu trữ các hằng số vào cùng một danh sách. Ví dụ:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
3
20 - Điều này đếm số lần đối số từ khóa xảy ra. Ví dụ, điều này rất hữu ích cho việc tăng mức độ dài dòng:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
4Lưu ý, mặc định sẽ là
26 trừ khi được đặt rõ ràng thành 0.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
21 - Điều này in một thông báo trợ giúp hoàn chỉnh cho tất cả các tùy chọn trong trình phân tích cú pháp hiện tại và sau đó thoát. Theo mặc định, một hành động trợ giúp được tự động thêm vào trình phân tích cú pháp. Xemimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
39 để biết chi tiết về cách tạo đầu ra.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
22 - Điều này mong đợi một đối số từ khóaimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
89 trong cuộc gọi
42 và in thông tin phiên bản và thoát khi được gọi:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
5parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
91 - Điều này lưu trữ một danh sách và mở rộng từng giá trị đối số cho danh sách. Ví dụ sử dụng:args = parser.parse_args[] print[args.filename, args.count, args.verbose]
6Mới trong phiên bản 3.8.
Bạn cũng có thể chỉ định một hành động tùy ý bằng cách chuyển một lớp con hành động hoặc đối tượng khác thực hiện cùng một giao diện.
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']92 có sẵn trong
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 và thêm hỗ trợ cho các hành động Boolean như parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']40 và
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']95:
args = parser.parse_args[] print[args.filename, args.count, args.verbose]7
Mới trong phiên bản 3.9.
Cách được đề xuất để tạo hành động tùy chỉnh là mở rộng
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']96, ghi đè phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']97 và tùy chọn các phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']98 và
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']99.
Một ví dụ về một hành động tùy chỉnh:
args = parser.parse_args[] print[args.filename, args.count, args.verbose]8
Để biết thêm chi tiết, xem
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']96.
nargs¶
Các đối tượng của ArgumpParser thường liên kết một đối số dòng lệnh duy nhất với một hành động duy nhất được thực hiện. Đối số từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag01 liên kết một số lượng đối số dòng lệnh khác với một hành động duy nhất. Các giá trị được hỗ trợ là:
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
02 [một số nguyên].parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
02 Đối số từ dòng lệnh sẽ được tập hợp lại với nhau thành một danh sách. Ví dụ:args = parser.parse_args[] print[args.filename, args.count, args.verbose]
9Lưu ý rằng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
04 tạo ra một danh sách một mục. Điều này khác với mặc định, trong đó mặt hàng được sản xuất bởi chính nó.
28. Một đối số sẽ được tiêu thụ từ dòng lệnh nếu có thể và được tạo ra như một mục duy nhất. Nếu không có đối số dòng lệnh nào có mặt, giá trị từ mặc định sẽ được tạo ra. Lưu ý rằng đối với các đối số tùy chọn, có một trường hợp bổ sung - chuỗi tùy chọn có mặt nhưng không được theo sau bởi một đối số dòng lệnh. Trong trường hợp này, giá trị từ const sẽ được sản xuất. Một số ví dụ để minh họa điều này:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
0Một trong những cách sử dụng phổ biến hơn của
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
06 là cho phép các tệp đầu vào và đầu ra tùy chọn:import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
1
29. Tất cả các đối số dòng lệnh có mặt được thu thập vào một danh sách. Lưu ý rằng thường không có ý nghĩa gì khi có nhiều đối số vị trí vớiimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
08, nhưng có thể có nhiều đối số tùy chọn vớiparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
08. Ví dụ:import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
2
30. Giống nhưimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
29, tất cả các arg dòng lệnh hiện tại được thu thập vào một danh sách. Ngoài ra, một thông báo lỗi sẽ được tạo nếu có ít nhất một đối số dòng lệnh. Ví dụ:import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
3
Nếu đối số từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag01 không được cung cấp, số lượng đối số được tiêu thụ được xác định bởi hành động. Nói chung, điều này có nghĩa là một đối số dòng lệnh duy nhất sẽ được tiêu thụ và một mục duy nhất [không phải danh sách] sẽ được tạo ra.
hăng sô¶
Đối số
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag13 của
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 được sử dụng để giữ các giá trị không đổi không được đọc từ dòng lệnh nhưng được yêu cầu cho các hành động import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 khác nhau. Hai cách sử dụng phổ biến nhất của nó là:Khi
42 được gọi vớiimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
17 hoặcparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
18. Các hành động này thêm giá trịparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
13 vào một trong các thuộc tính của đối tượng được trả về bởi
44. Xem mô tả hành động cho các ví dụ. Nếuimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
13 không được cung cấp cho
42, nó sẽ nhận được giá trị mặc định làimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
26.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Khi
42 được gọi với các chuỗi tùy chọn [nhưimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
39 hoặcparser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
40] vàparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
06. Điều này tạo ra một đối số tùy chọn có thể được theo sau bởi 0 hoặc một đối số dòng lệnh. Khi phân tích các dòng lệnh, nếu chuỗi tùy chọn gặp phải không có đối số dòng lệnh theo nó, giá trị củaparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
13 sẽ được coi là
26 thay thế. Xem mô tả NARGS cho các ví dụ.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Đã thay đổi trong phiên bản 3.11: parser.add_argument['filename'] # positional argument
parser.add_argument['-c', '--count'] # option that takes a value
parser.add_argument['-v', '--verbose',
action='store_true'] # on/off flag
30 theo mặc định, bao gồm cả khi parser.add_argument['filename'] # positional argument
parser.add_argument['-c', '--count'] # option that takes a value
parser.add_argument['-v', '--verbose',
action='store_true'] # on/off flag
18 hoặc parser.add_argument['filename'] # positional argument
parser.add_argument['-c', '--count'] # option that takes a value
parser.add_argument['-v', '--verbose',
action='store_true'] # on/off flag
17.
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag30 by default, including when
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag18 or
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag17.
mặc định¶
Tất cả các đối số tùy chọn và một số đối số vị trí có thể được bỏ qua tại dòng lệnh. Đối số từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag33 của
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42, có giá trị mặc định là import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
26, chỉ định giá trị nào nên được sử dụng nếu không có đối số dòng lệnh. Đối với các đối số tùy chọn, giá trị parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag33 được sử dụng khi chuỗi tùy chọn không có ở dòng lệnh:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]4
Nếu không gian tên đích đã có một bộ thuộc tính, mặc định hành động sẽ không ghi được quá nhiều:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]5
Nếu giá trị
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag33 là một chuỗi, trình phân tích cú pháp phân tích giá trị như thể nó là đối số dòng lệnh. Cụ thể, trình phân tích cú pháp áp dụng bất kỳ đối số chuyển đổi loại nào, nếu được cung cấp, trước khi đặt thuộc tính trên giá trị trả về
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
55. Mặt khác, trình phân tích cú pháp sử dụng giá trị như là:import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]6
Đối với các đối số vị trí có NARG bằng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag39 hoặc
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag40, giá trị
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag33 được sử dụng khi không có đối số dòng lệnh nào:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]7
Cung cấp
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag42 khiến không có thuộc tính nào được thêm vào nếu đối số dòng lệnh không có mặt:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]8
loại hình¶
Theo mặc định, trình phân tích cú pháp đọc các đối số dòng lệnh trong các chuỗi đơn giản. Tuy nhiên, khá thường thì chuỗi dòng lệnh thay vào đó nên được hiểu là một loại khác, chẳng hạn như
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
35 hoặc import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
27. Từ khóa parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag45 cho
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 cho phép thực hiện mọi chuyển đổi kiểm tra loại và kiểm tra loại cần thiết.Nếu từ khóa loại được sử dụng với từ khóa mặc định, bộ chuyển đổi loại chỉ được áp dụng nếu mặc định là một chuỗi.
Đối số của
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag45 có thể là bất kỳ cuộc gọi nào có thể gọi được chấp nhận một chuỗi. Nếu hàm tăng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag48,
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag49 hoặc
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag50, ngoại lệ sẽ bị bắt và thông báo lỗi được định dạng độc đáo được hiển thị. Không có loại ngoại lệ khác được xử lý.
Các loại và chức năng tích hợp phổ biến có thể được sử dụng làm bộ chuyển đổi loại:
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]9
Các chức năng do người dùng xác định cũng có thể được sử dụng:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]0
Hàm
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag51 không được khuyến nghị làm bộ chuyển đổi loại. Tất cả những gì nó làm là chuyển đổi các chuỗi trống thành
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
33 và các chuỗi không trống thành import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
32. Đây thường không phải là những gì mong muốn.Nói chung, từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag45 là một sự tiện lợi chỉ nên được sử dụng cho các chuyển đổi đơn giản chỉ có thể tăng một trong ba ngoại lệ được hỗ trợ. Bất cứ điều gì có xử lý lỗi hoặc quản lý tài nguyên thú vị hơn nên được thực hiện ở hạ lưu sau khi các đối số được phân tích cú pháp.
Ví dụ: chuyển đổi JSON hoặc YAML có các trường hợp lỗi phức tạp yêu cầu báo cáo tốt hơn so với từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag45. Một
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag56 sẽ không được định dạng tốt và ngoại lệ
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag57 sẽ không được xử lý.
Ngay cả
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 cũng có những hạn chế để sử dụng với từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag45. Nếu một đối số sử dụng FileType và sau đó một đối số tiếp theo không thành công, một lỗi được báo cáo nhưng tệp không được tự động đóng. Trong trường hợp này, sẽ tốt hơn để đợi cho đến khi trình phân tích cú pháp đã chạy và sau đó sử dụng ________ 360-statement để quản lý các tệp.
Đối với trình kiểm tra loại chỉ cần kiểm tra đối với một tập hợp các giá trị cố định, hãy xem xét sử dụng từ khóa lựa chọn thay thế.
Lựa chọn lor
Một số đối số dòng lệnh nên được chọn từ một tập hợp các giá trị bị hạn chế. Chúng có thể được xử lý bằng cách chuyển một đối tượng container làm đối số từ khóa lựa chọn cho
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42. Khi dòng lệnh được phân tích cú pháp, các giá trị đối số sẽ được kiểm tra và thông báo lỗi sẽ được hiển thị nếu đối số không phải là một trong những giá trị chấp nhận được:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]1
Lưu ý rằng việc đưa vào thùng chứa lựa chọn được kiểm tra sau khi bất kỳ chuyển đổi loại nào đã được thực hiện, do đó, loại đối tượng trong thùng chứa lựa chọn phải khớp với loại được chỉ định:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]2
Bất kỳ container nào cũng có thể được truyền dưới dạng giá trị lựa chọn, do đó, các đối tượng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag62, đối tượng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag63 và các thùng chứa tùy chỉnh đều được hỗ trợ.
Việc sử dụng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag64 không được khuyến nghị vì rất khó kiểm soát sự xuất hiện của nó trong việc sử dụng, trợ giúp và thông báo lỗi.
Các lựa chọn được định dạng ghi đè Metavar mặc định thường có nguồn gốc từ Dest. Đây thường là những gì bạn muốn vì người dùng không bao giờ nhìn thấy tham số Dests. Nếu màn hình này không phải là mong muốn [có lẽ vì có nhiều lựa chọn], chỉ cần chỉ định một Metavar rõ ràng.
yêu cầu¶
Nói chung, mô -đun
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 giả định rằng các cờ như parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']39 và
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag67 chỉ ra các đối số tùy chọn, luôn có thể được bỏ qua ở dòng lệnh. Để thực hiện một tùy chọn yêu cầu,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
32 có thể được chỉ định cho đối số từ khóa parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag69 thành
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42:$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]3
Như ví dụ cho thấy, nếu một tùy chọn được đánh dấu là
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag71,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 sẽ báo cáo lỗi nếu tùy chọn đó không có ở dòng lệnh.Ghi chú
Các tùy chọn cần thiết thường được coi là hình thức xấu vì người dùng mong đợi các tùy chọn là tùy chọn và do đó chúng nên tránh khi có thể.
Cứu giúp¶
Giá trị
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag73 là một chuỗi chứa một mô tả ngắn gọn về đối số. Khi người dùng yêu cầu trợ giúp [thường là bằng cách sử dụng
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']42 hoặc
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']43 tại dòng lệnh], các mô tả
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag73 này sẽ được hiển thị với mỗi đối số:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]4
Các chuỗi
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag73 có thể bao gồm các định dạng định dạng khác nhau để tránh lặp lại những thứ như tên chương trình hoặc mặc định đối số. Các nhà xác định có sẵn bao gồm tên chương trình,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
78 và hầu hết các đối số từ khóa đến import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42, ví dụ: parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag80,
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag81, vv .:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]5
Khi chuỗi trợ giúp hỗ trợ %-formatting, nếu bạn muốn một
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag82 theo nghĩa đen xuất hiện trong chuỗi trợ giúp, bạn phải thoát nó dưới dạng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag83.
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 hỗ trợ làm im lặng mục trợ giúp cho một số tùy chọn nhất định, bằng cách đặt giá trị parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag73 thành
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag86:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]6
Metavar¶
Khi
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 tạo tin nhắn trợ giúp, nó cần một số cách để đề cập đến từng đối số dự kiến. Theo mặc định, các đối tượng ArgentParSer sử dụng giá trị định mệnh làm tên tên của mỗi đối tượng. Theo mặc định, đối với các hành động đối số vị trí, giá trị định mệnh được sử dụng trực tiếp và đối với các hành động đối số tùy chọn, giá trị định mệnh được sử dụng trên đường. Vì vậy, một đối số vị trí duy nhất với parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag88 sẽ được gọi là
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag89. Một đối số tùy chọn duy nhất
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']40 phải được theo sau bởi một đối số dòng lệnh duy nhất sẽ được gọi là
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag91. Một ví dụ:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]7
Một tên thay thế có thể được chỉ định với
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag92:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]8
Lưu ý rằng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag92 chỉ thay đổi tên được hiển thị - tên của thuộc tính trên đối tượng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 vẫn được xác định bởi giá trị định mệnh.Các giá trị khác nhau của
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag01 có thể khiến Metavar được sử dụng nhiều lần. Cung cấp một tuple cho
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag92 Chỉ định một màn hình khác nhau cho từng đối số:
$ python prog.py -h usage: prog.py [-h] [--sum] N [N ...] Process some integers. positional arguments: N an integer for the accumulator options: -h, --help show this help message and exit --sum sum the integers [default: find the max]9
định mệnh
Hầu hết các hành động
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Thêm một số giá trị dưới dạng thuộc tính của đối tượng được trả về bởi import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44. Tên của thuộc tính này được xác định bởi đối số từ khóa parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 của
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42. Đối với các hành động đối số vị trí, parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 thường được cung cấp là đối số đầu tiên cho
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42:$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 100
Đối với các hành động đối số tùy chọn, giá trị của
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 thường được suy ra từ các chuỗi tùy chọn.
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 tạo ra giá trị của parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 bằng cách lấy chuỗi tùy chọn dài đầu tiên và loại bỏ chuỗi
args = parser.parse_args[] print[args.filename, args.count, args.verbose]06 ban đầu. Nếu không có chuỗi tùy chọn dài nào được cung cấp,
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 sẽ được lấy từ chuỗi tùy chọn ngắn đầu tiên bằng cách tước ký tự
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 ban đầu. Bất kỳ ký tự
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 nào sẽ được chuyển đổi thành các ký tự
args = parser.parse_args[] print[args.filename, args.count, args.verbose]10 để đảm bảo chuỗi là tên thuộc tính hợp lệ. Các ví dụ dưới đây minh họa hành vi này:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 101
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 cho phép một tên thuộc tính tùy chỉnh được cung cấp:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 102
Các lớp hành động
Các lớp hành động thực hiện API hành động, một cuộc gọi có thể gọi được trả về một cuộc gọi có thể gọi được xử lý các đối số từ dòng lệnh. Bất kỳ đối tượng nào theo API này có thể được truyền dưới dạng tham số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']68 đến
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42. classargparse.action [tùy chọn_strings, dest, nargs = none, const = none, mặc định = không, loại = không, lựa chọn = none, bắt buộc = falseargparse.Action[option_strings, dest,
nargs=None, const=None, default=None, type=None, choices=None,
required=False, help=None, metavar=None]¶Các đối tượng hành động được sử dụng bởi một archarchParser để thể hiện thông tin cần thiết để phân tích một đối số duy nhất từ một hoặc nhiều chuỗi từ dòng lệnh. Lớp hành động phải chấp nhận hai đối số vị trí cộng với bất kỳ đối số từ khóa nào được chuyển đến
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
12 ngoại trừ chính parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']68.
Các phiên bản hành động [hoặc giá trị trả về của bất kỳ cuộc gọi nào có thể gọi được cho tham số
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']68] nên có các thuộc tính của Dest Dest, Tùy chọn_Strings Cách dễ nhất để đảm bảo các thuộc tính này được xác định là gọi
args = parser.parse_args[] print[args.filename, args.count, args.verbose]17.
Các trường hợp hành động phải được gọi, vì vậy các lớp con phải ghi đè phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']97, cần chấp nhận bốn tham số:
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
19 - Đối tượng ArgentParser chứa hành động này.args = parser.parse_args[] print[args.filename, args.count, args.verbose]
20 - Đối tượng
55 sẽ được trả về bởiimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
44. Hầu hết các hành động thêm một thuộc tính cho đối tượng này bằng cách sử dụngimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
23.args = parser.parse_args[] print[args.filename, args.count, args.verbose]
24 - Các đối số dòng lệnh liên quan, với bất kỳ chuyển đổi loại nào được áp dụng. Chuyển đổi loại được chỉ định với đối số từ khóa loại thành
42.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
args = parser.parse_args[] print[args.filename, args.count, args.verbose]
26 - Chuỗi tùy chọn được sử dụng để gọi hành động này. Đối sốargs = parser.parse_args[] print[args.filename, args.count, args.verbose]
26 là tùy chọn và sẽ vắng mặt nếu hành động được liên kết với một đối số vị trí.
Phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']97 có thể thực hiện các hành động tùy ý, nhưng thường sẽ đặt các thuộc tính trên
args = parser.parse_args[] print[args.filename, args.count, args.verbose]20 dựa trên
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 và
args = parser.parse_args[] print[args.filename, args.count, args.verbose]24.
Các lớp con hành động có thể xác định phương thức
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']99 không có đối số và trả về một chuỗi sẽ được sử dụng khi in cách sử dụng của chương trình. Nếu phương thức đó không được cung cấp, một mặc định hợp lý sẽ được sử dụng.
Phương thức parse_args []
ArcharchParser.parse_args [args = none, không gian tên = none] ¶parse_args[args=None, namespace=None]¶Chuyển đổi chuỗi đối số thành các đối tượng và gán chúng là thuộc tính của không gian tên. Trả lại không gian tên đông dân.
Các cuộc gọi trước đây đến
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 Xác định chính xác những đối tượng nào được tạo và cách chúng được gán. Xem tài liệu cho import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 để biết chi tiết.Args - Danh sách các chuỗi để phân tích cú pháp. Mặc định được lấy từ
08.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Không gian tên - một đối tượng để lấy các thuộc tính. Mặc định là một đối tượng
55 trống mới.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Giá trị tùy chọn Cú pháp
Phương thức
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 hỗ trợ một số cách chỉ định giá trị của một tùy chọn [nếu nó có một]. Trong trường hợp đơn giản nhất, tùy chọn và giá trị của nó được truyền dưới dạng hai đối số riêng biệt:$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 103
Đối với các tùy chọn dài [các tùy chọn có tên dài hơn một ký tự], tùy chọn và giá trị cũng có thể được truyền dưới dạng đối số dòng lệnh duy nhất, sử dụng
args = parser.parse_args[] print[args.filename, args.count, args.verbose]38 để tách chúng ra:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 104
Đối với các tùy chọn ngắn [các tùy chọn chỉ dài một ký tự], tùy chọn và giá trị của nó có thể được nối:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 105
Một số tùy chọn ngắn có thể được kết hợp với nhau, chỉ sử dụng một tiền tố
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 duy nhất, miễn là chỉ có tùy chọn cuối cùng [hoặc không ai trong số chúng] yêu cầu một giá trị:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 106
Đối số không hợp lệ¶
Trong khi phân tích các dòng lệnh,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 kiểm tra nhiều lỗi khác nhau, bao gồm các tùy chọn mơ hồ, các loại không hợp lệ, các tùy chọn không hợp lệ, số lượng đối số vị trí sai, v.v. :$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 107
Đối số chứa ________ 206¶
Phương pháp
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 cố gắng đưa ra lỗi bất cứ khi nào người dùng rõ ràng đã phạm sai lầm, nhưng một số tình huống vốn đã mơ hồ. Ví dụ: đối số dòng lệnh args = parser.parse_args[] print[args.filename, args.count, args.verbose]43 có thể là một nỗ lực để chỉ định một tùy chọn hoặc cố gắng cung cấp một đối số vị trí. Phương pháp
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 rất thận trọng ở đây: Đối số vị trí chỉ có thể bắt đầu bằng parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 nếu chúng trông giống như số âm và không có tùy chọn nào trong trình phân tích cú pháp trông giống như số âm:
$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 108
Nếu bạn có các đối số vị trí phải bắt đầu với
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']06 và don lồng trông giống như các số âm, bạn có thể chèn các đối số giả
args = parser.parse_args[] print[args.filename, args.count, args.verbose]47 cho biết
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 rằng mọi thứ sau đó là một đối số vị trí:$ python prog.py 1 2 3 4 4 $ python prog.py 1 2 3 4 --sum 109
Các chữ viết tắt đối số [khớp tiền tố] ¶
Phương pháp
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 theo mặc định cho phép các tùy chọn dài được viết tắt là tiền tố, nếu viết tắt không rõ ràng [tiền tố phù hợp với tùy chọn duy nhất]:by default allows long options to be abbreviated to a prefix, if the abbreviation is unambiguous [the prefix matches a unique option]:$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'0
Một lỗi được tạo ra cho các đối số có thể tạo ra nhiều tùy chọn. Tính năng này có thể được vô hiệu hóa bằng cách đặt allow_abbrev thành
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
33.allow_abbrev to import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
33.Ngoài ________ 108¶
Đôi khi có thể hữu ích khi có một đối số phân tích phân tích khác với các đối số của
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
08. Điều này có thể được thực hiện bằng cách chuyển một danh sách các chuỗi cho import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44. Điều này rất hữu ích để kiểm tra tại dấu nhắc tương tác:$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'1
Đối tượng không gian tên
classargparse.namespace¶argparse.Namespace¶Lớp đơn giản được sử dụng theo mặc định bởi
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 để tạo các thuộc tính giữ đối tượng và trả về nó.Lớp này có chủ ý đơn giản, chỉ là một lớp con
args = parser.parse_args[] print[args.filename, args.count, args.verbose]55 với một biểu diễn chuỗi có thể đọc được. Nếu bạn thích có chế độ xem giống như các thuộc tính, bạn có thể sử dụng thành ngữ Python tiêu chuẩn,
args = parser.parse_args[] print[args.filename, args.count, args.verbose]56:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'2
Cũng có thể hữu ích khi có một gán
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 gán cho một đối tượng đã có, thay vì một đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
55 mới. Điều này có thể đạt được bằng cách chỉ định đối số từ khóa args = parser.parse_args[] print[args.filename, args.count, args.verbose]59:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'3
Các tiện ích khác
Chỉ huy phụ công
ArcharchParser.add_subparsers [[Tiêu đề] [, Mô tả] [, Prog] [, Parser_Class] [, Action] [, Tùy chọn_Strings] [, Dest] [, Yêu cầu] [, Help] [, Metavar]] ¶add_subparsers[[title][, description][, prog][, parser_class][, action][, option_strings][, dest][, required][, help][, metavar]]¶Nhiều chương trình chia chức năng của chúng thành một số lệnh phụ, ví dụ, chương trình
args = parser.parse_args[] print[args.filename, args.count, args.verbose]60 có thể gọi các lệnh phụ như
args = parser.parse_args[] print[args.filename, args.count, args.verbose]61,
args = parser.parse_args[] print[args.filename, args.count, args.verbose]62 và
args = parser.parse_args[] print[args.filename, args.count, args.verbose]63. Chia tách chức năng theo cách này có thể là một ý tưởng đặc biệt tốt khi một chương trình thực hiện một số chức năng khác nhau yêu cầu các loại đối số dòng lệnh khác nhau.
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 hỗ trợ việc tạo ra các lệnh phụ như vậy bằng phương pháp args = parser.parse_args[] print[args.filename, args.count, args.verbose]65. Phương thức
args = parser.parse_args[] print[args.filename, args.count, args.verbose]65 thường được gọi là không có đối số và trả về một đối tượng hành động đặc biệt. Đối tượng này có một phương thức duy nhất,
args = parser.parse_args[] print[args.filename, args.count, args.verbose]67, lấy tên lệnh và bất kỳ đối số trình xây dựng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 nào và trả về một đối tượng import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 có thể được sửa đổi như bình thường.Mô tả các tham số:
Tiêu đề - Tiêu đề cho nhóm phân tích phụ trong đầu ra trợ giúp; Theo mặc định, các tiểu ban, "nếu mô tả được cung cấp, nếu không thì sử dụng tiêu đề cho các đối số vị trí
Mô tả - Mô tả cho nhóm phân tích phụ trong đầu ra trợ giúp, theo mặc định
26import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Prog - Thông tin sử dụng sẽ được hiển thị với Trợ giúp phụ, theo mặc định, tên của chương trình và bất kỳ đối số vị trí nào trước đối số phụ
Parser_Class - Lớp sẽ được sử dụng để tạo các phiên bản phân tích phụ, theo mặc định, lớp của trình phân tích cú pháp hiện tại [ví dụ: ArgumentParSer]
Hành động - Loại hành động cơ bản sẽ được thực hiện khi gặp đối số này ở dòng lệnh
Dest - Tên của thuộc tính theo đó tên phụ sẽ được lưu trữ; theo mặc định
26 và không có giá trị nào được lưu trữimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Yêu cầu - Có hay không một tiểu ban phải được cung cấp, theo mặc định
33 [được thêm vào trong 3.7]import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Trợ giúp - Trợ giúp cho nhóm phân tích phụ trong đầu ra trợ giúp, theo mặc định
26import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Metavar - Chuỗi trình bày các lệnh phụ có sẵn trong trợ giúp; Theo mặc định, đó là
26 và trình bày các lệnh phụ ở dạng {cmd1, cmd2, ..}import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Một số ví dụ sử dụng:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'4
Lưu ý rằng đối tượng được trả về bởi
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 sẽ chỉ chứa các thuộc tính cho trình phân tích cú pháp chính và trình con được chọn bởi dòng lệnh [chứ không phải bất kỳ trình nào khác]. Vì vậy, trong ví dụ trên, khi lệnh args = parser.parse_args[] print[args.filename, args.count, args.verbose]76 được chỉ định, chỉ có các thuộc tính
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']57 và
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag89 và khi lệnh
args = parser.parse_args[] print[args.filename, args.count, args.verbose]79 được chỉ định, chỉ có các thuộc tính
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']57 và
args = parser.parse_args[] print[args.filename, args.count, args.verbose]81.
Tương tự, khi một tin nhắn trợ giúp được yêu cầu từ trình con, chỉ có sự trợ giúp cho trình phân tích cú pháp cụ thể đó mới được in. Thông điệp trợ giúp sẽ không bao gồm trình phân tích cú pháp phụ huynh hoặc trình phân tích cú pháp anh chị em. .
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'5
Phương pháp
args = parser.parse_args[] print[args.filename, args.count, args.verbose]65 cũng hỗ trợ các đối số từ khóa
args = parser.parse_args[] print[args.filename, args.count, args.verbose]85 và
args = parser.parse_args[] print[args.filename, args.count, args.verbose]86. Khi có mặt, các lệnh Subparser sẽ xuất hiện trong nhóm của riêng họ trong đầu ra trợ giúp. Ví dụ:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'6
Hơn nữa,
args = parser.parse_args[] print[args.filename, args.count, args.verbose]87 hỗ trợ một đối số
args = parser.parse_args[] print[args.filename, args.count, args.verbose]88 bổ sung, cho phép nhiều chuỗi tham khảo cùng một trình tự phụ. Ví dụ này, như
args = parser.parse_args[] print[args.filename, args.count, args.verbose]60, bí danh
args = parser.parse_args[] print[args.filename, args.count, args.verbose]90 như một tốc ký cho
args = parser.parse_args[] print[args.filename, args.count, args.verbose]91:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'7
Một cách đặc biệt hiệu quả để xử lý các lệnh phụ là kết hợp việc sử dụng phương thức
args = parser.parse_args[] print[args.filename, args.count, args.verbose]65 với các cuộc gọi đến
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']23 để mỗi người con biết chức năng Python nào nên thực hiện. Ví dụ:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'8
Bằng cách này, bạn có thể để
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 thực hiện công việc gọi chức năng thích hợp sau khi phân tích đối số hoàn tất. Liên kết các chức năng với các hành động như thế này thường là cách dễ nhất để xử lý các hành động khác nhau cho mỗi người phụ của bạn. Tuy nhiên, nếu cần phải kiểm tra tên của trình con được gọi, đối số từ khóa parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag99 cho cuộc gọi
args = parser.parse_args[] print[args.filename, args.count, args.verbose]65 sẽ hoạt động:
$ python prog.py a b c usage: prog.py [-h] [--sum] N [N ...] prog.py: error: argument N: invalid int value: 'a'9
Thay đổi trong phiên bản 3.7: Đối số từ khóa bắt buộc mới.New required keyword argument.
Đối tượng Filetype
classargparse.filetype [mode = 'r', bufsize = -1, expoding = none, error = none] ¶ argparse.FileType[mode='r', bufsize=- 1, encoding=None, errors=None]¶Nhà máy
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 tạo ra các đối tượng có thể được chuyển đến đối số loại
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
12. Các đối số có các đối tượng parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 là loại của chúng sẽ mở các đối số dòng lệnh dưới dạng các tệp với các chế độ được yêu cầu, kích thước bộ đệm, mã hóa và xử lý lỗi [xem hàm
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]00 để biết thêm chi tiết]:
>>> parser = argparse.ArgumentParser[description='Process some integers.']0
Các đối tượng FileType hiểu đối số giả
parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']12 và tự động chuyển đổi nó thành
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]02 cho các đối tượng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 có thể đọc được và
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]04 cho các đối tượng
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag58 có thể ghi:
>>> parser = argparse.ArgumentParser[description='Process some integers.']1
Mới trong phiên bản 3.4: Các đối số từ khóa mã hóa và lỗi.The encodings and errors keyword arguments.
Nhóm tranh luận
ArcharchParser.add_argument_group [title = none, description = none] ¶add_argument_group[title=None, description=None]¶Theo mặc định,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 Các đối số dòng lệnh của các nhóm thành các đối số vị trí của các đối số và các đối số tùy chọn, khi hiển thị các thông báo trợ giúp. Khi có một nhóm đối số khái niệm tốt hơn so với mặc định này, các nhóm thích hợp có thể được tạo bằng phương pháp import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]07:
>>> parser = argparse.ArgumentParser[description='Process some integers.']2
Phương thức
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]07 trả về một đối tượng nhóm đối số có phương thức
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 giống như import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 thông thường. Khi một đối số được thêm vào nhóm, trình phân tích cú pháp đối xử với nó giống như một đối số bình thường, nhưng hiển thị đối số trong một nhóm riêng cho các tin nhắn trợ giúp. Phương thức import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]07 chấp nhận các đối số tiêu đề và mô tả có thể được sử dụng để tùy chỉnh màn hình này:
>>> parser = argparse.ArgumentParser[description='Process some integers.']3
Lưu ý rằng bất kỳ đối số nào không có trong các nhóm do người dùng định nghĩa sẽ kết thúc trong các phần đối số vị trí thông thường của các phần và các đối số tùy chọn.
Đã thay đổi trong phiên bản 3.11: Gọi import argparse
parser = argparse.ArgumentParser[description='Process some integers.']
parser.add_argument['integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator']
parser.add_argument['--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers [default: find the max]']
args = parser.parse_args[]
print[args.accumulate[args.integers]]
07 trên một nhóm đối số bị phản đối. Tính năng này không bao giờ được hỗ trợ và không phải lúc nào cũng hoạt động chính xác. Chức năng tồn tại trên API do tai nạn thông qua kế thừa và sẽ bị xóa trong tương lai.Calling
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]07 on an argument group is deprecated. This feature was never supported and does not always work correctly. The function exists on the API by accident through inheritance and will be removed in the future.
Loại trừ lẫn nhau¶
ArcharchParser.add_mutally_exclusive_group [bắt buộc = false] ¶add_mutually_exclusive_group[required=False]¶Tạo một nhóm loại trừ lẫn nhau.
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 sẽ đảm bảo rằng chỉ có một trong các đối số trong nhóm loại trừ lẫn nhau có mặt trên dòng lệnh:>>> parser = argparse.ArgumentParser[description='Process some integers.']4
Phương pháp
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]14 cũng chấp nhận một đối số cần thiết, để chỉ ra rằng ít nhất một trong các đối số loại trừ lẫn nhau là bắt buộc:
>>> parser = argparse.ArgumentParser[description='Process some integers.']5
Lưu ý rằng các nhóm đối số loại trừ hiện tại không hỗ trợ tiêu đề và mô tả đối số của
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]07.
Đã thay đổi trong phiên bản 3.11: Gọi import argparse
parser = argparse.ArgumentParser[description='Process some integers.']
parser.add_argument['integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator']
parser.add_argument['--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers [default: find the max]']
args = parser.parse_args[]
print[args.accumulate[args.integers]]
07 hoặc import argparse
parser = argparse.ArgumentParser[description='Process some integers.']
parser.add_argument['integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator']
parser.add_argument['--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers [default: find the max]']
args = parser.parse_args[]
print[args.accumulate[args.integers]]
14 trên một nhóm loại trừ lẫn nhau không được chấp nhận. Những tính năng này không bao giờ được hỗ trợ và không phải lúc nào cũng hoạt động chính xác. Các chức năng tồn tại trên API do tai nạn thông qua kế thừa và sẽ bị xóa trong tương lai.Calling
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]07 or
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]14 on a mutually exclusive group is deprecated. These features were never supported and do not always work correctly. The functions exist on the API by accident through inheritance and will be removed in the future.
Người phân tích cú pháp Defaults¶
ArcharchParser.set_defaults [** kwargs] ¶set_defaults[**kwargs]¶Hầu hết thời gian, các thuộc tính của đối tượng được trả về bởi
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 sẽ được xác định đầy đủ bằng cách kiểm tra các đối số dòng lệnh và các hành động đối số. parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']23 cho phép một số thuộc tính bổ sung được xác định mà không cần thêm bất kỳ sự kiểm tra nào của dòng lệnh:
>>> parser = argparse.ArgumentParser[description='Process some integers.']6
Lưu ý rằng các mặc định cấp độ phân tích cú pháp luôn ghi đè mặc định cấp đối số:
>>> parser = argparse.ArgumentParser[description='Process some integers.']7
Mặc định cấp độ phân tích cú pháp có thể đặc biệt hữu ích khi làm việc với nhiều trình phân tích cú pháp. Xem phương thức
args = parser.parse_args[] print[args.filename, args.count, args.verbose]65 để biết ví dụ về loại này. Archarchparparser.get_default [Dest] ¶get_default[dest]¶
Nhận giá trị mặc định cho thuộc tính không gian tên, như được đặt bởi
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
42 hoặc bằng parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']23:
>>> parser = argparse.ArgumentParser[description='Process some integers.']8
In ấn trợ giúp
Trong hầu hết các ứng dụng điển hình,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 sẽ chăm sóc định dạng và in bất kỳ thông báo sử dụng hoặc lỗi nào. Tuy nhiên, một số phương thức định dạng có sẵn: ArcharchParser.print_usage [file = none] ¶print_usage[file=None]¶In một mô tả ngắn gọn về cách
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 nên được gọi trên dòng lệnh. Nếu tệp là import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
26, import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]04 được giả định. ArcharchParser.print_help [file = none] ¶print_help[file=None]¶
In tin nhắn trợ giúp, bao gồm sử dụng chương trình và thông tin về các đối số được đăng ký với
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39. Nếu tệp là import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
26, import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]04 được giả định.
Ngoài ra còn có các biến thể của các phương thức này chỉ cần trả về một chuỗi thay vì in nó:
ArcharchParser.format_usage [] ¶format_usage[]¶Trả về một chuỗi chứa một mô tả ngắn gọn về cách
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39 nên được gọi trên dòng lệnh. ArcharchParser.format_help [] ¶format_help[]¶Trả về một chuỗi chứa thông báo trợ giúp, bao gồm việc sử dụng chương trình và thông tin về các đối số được đăng ký với
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39.Phân tích cú pháp một phần
ArcharchParser.parse_known_args [args = none, không gian tên = none] ¶parse_known_args[args=None, namespace=None]¶Đôi khi một tập lệnh chỉ có thể phân tích một vài đối số dòng lệnh, chuyển các đối số còn lại cho tập lệnh hoặc chương trình khác. Trong những trường hợp này, phương pháp
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]32 có thể hữu ích. Nó hoạt động giống như
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
44 ngoại trừ việc nó không tạo ra lỗi khi có các đối số bổ sung. Thay vào đó, nó trả về một tuple hai mục chứa không gian tên dân cư và danh sách các chuỗi đối số còn lại.>>> parser = argparse.ArgumentParser[description='Process some integers.']9
Cảnh báo
Các quy tắc khớp tiền tố áp dụng cho import argparse
parser = argparse.ArgumentParser[description='Process some integers.']
parser.add_argument['integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator']
parser.add_argument['--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers [default: find the max]']
args = parser.parse_args[]
print[args.accumulate[args.integers]]
32. Trình phân tích cú pháp có thể tiêu thụ một tùy chọn ngay cả khi nó chỉ là tiền tố của một trong những tùy chọn đã biết của nó, thay vì để nó trong danh sách đối số còn lại. rules apply to
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]32. The parser may consume an option even if it’s just a prefix of one of its known options, instead of leaving it in the remaining arguments list.
Tùy chỉnh tệp phân tích cú pháp tệp
ArmentParser.convert_arg_line_to_args [arg_line] ¶convert_arg_line_to_args[arg_line]¶Các đối số được đọc từ một tệp [xem đối số từ khóa từfile_prefix_chars đến trình xây dựng
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
39] được đọc một đối số trên mỗi dòng. parser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']17 có thể được ghi đè cho việc đọc fancier.
Phương thức này lấy một đối số arg_line là một chuỗi được đọc từ tệp đối số. Nó trả về một danh sách các đối số được phân tích cú pháp từ chuỗi này. Phương thức được gọi là một lần trên mỗi dòng đọc từ tệp đối số, theo thứ tự.
Một sự ghi đè hữu ích của phương pháp này là một loại coi từng từ được phân tách không gian như một đối số. Ví dụ sau đây cho thấy cách làm điều này:
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
00Phương pháp thoát
ArcharchParser.exit [status = 0, message = none] ¶exit[status=0, message=None]¶Phương thức này chấm dứt chương trình, thoát với trạng thái được chỉ định và, nếu được đưa ra, nó sẽ in một thông báo trước đó. Người dùng có thể ghi đè phương thức này để xử lý các bước này khác nhau:
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
01
ArcharchParser.Error [tin nhắn] ¶error[message]¶Phương pháp này in một thông báo sử dụng bao gồm thông báo đến lỗi tiêu chuẩn và chấm dứt chương trình bằng mã trạng thái là 2.
Phân tích cú pháp xen kẽ
ArcharchParser.parse_intermixed_args [args = none, không gian tên = không]parse_intermixed_args[args=None, namespace=None]¶ ArgumentParser.parse_known_intermixed_args[args=None, namespace=None]¶Một số lệnh UNIX cho phép người dùng xen kẽ các đối số tùy chọn với các đối số vị trí. Các phương pháp
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]37 và
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]38 hỗ trợ phong cách phân tích cú pháp này.
Các trình phân tích cú pháp này không hỗ trợ tất cả các tính năng của Argparse và sẽ tăng các ngoại lệ nếu các tính năng không được hỗ trợ được sử dụng. Cụ thể, các nhóm phụ,
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
31 và các nhóm loại trừ lẫn nhau bao gồm cả hai tùy chọn và vị trí không được hỗ trợ.Ví dụ sau đây cho thấy sự khác biệt giữa
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]32 và
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]37: cái trước trả về
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]42 là các đối số không được phân biệt, trong khi cái sau thu thập tất cả các vị trí vào
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]43.
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
02import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]38 Trả về một bộ hai mục chứa không gian tên dân cư và danh sách các chuỗi đối số còn lại.
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]37 gây ra lỗi nếu có bất kỳ chuỗi đối số không được nhìn nhận nào còn lại.
Mới trong phiên bản 3.7.
Nâng cấp mã OPTPARE
Ban đầu, mô -đun
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 đã cố gắng duy trì khả năng tương thích với import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]47. Tuy nhiên,
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]47 rất khó để mở rộng một cách minh bạch, đặc biệt là với những thay đổi cần thiết để hỗ trợ các nhà xác định
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]49 mới và thông báo sử dụng tốt hơn. Khi hầu hết mọi thứ trong
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]47 đã được sao chép hoặc được gắn khỉ, nó dường như không còn thực tế khi cố gắng duy trì khả năng tương thích ngược.
Mô -đun
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06 được cải thiện trên mô -đun thư viện tiêu chuẩn import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]47 theo một số cách bao gồm:
Xử lý đối số vị trí.
Hỗ trợ các lệnh phụ.
Cho phép các tiền tố tùy chọn thay thế như
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
53 vàimport argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
54.Xử lý các đối số theo phong cách không hoặc hoặc một hoặc nhiều hơn.
Sản xuất nhiều thông điệp sử dụng nhiều thông tin hơn.
Cung cấp một giao diện đơn giản hơn nhiều cho tùy chỉnh
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
45 vàparser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
68.
Đường dẫn nâng cấp một phần từ
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]47 lên
import requests
from bs4 import BeautifulSoup
response = requests.get["//stackoverflow.com/questions"]
soup = BeautifulSoup[response.text,"parser.html"]
questions = soup.select[".question-summary"]
06:Thay thế tất cả các cuộc gọi
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
59 bằng các cuộc gọi
12.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Thay thế
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
61 bằngimport argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
62 và thêm các cuộc gọi
12 bổ sung cho các đối số vị trí. Hãy nhớ rằng những gì trước đây được gọi làimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
64, bây giờ trong bối cảnh
06 được gọi làimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
66.Thay thế
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
67 bằng cách sử dụngimport argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
37 thay vì
44.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Thay thế các hành động gọi lại và các đối số từ khóa
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
70 bằng các đối sốparser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
45 hoặcparser = argparse.ArgumentParser[ prog = 'ProgramName', description = 'What the program does', epilog = 'Text at the bottom of help']
68.Thay thế tên chuỗi cho các đối số từ khóa
parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
45 bằng các đối tượng loại tương ứng [ví dụ: int, float, phức tạp, v.v.].Thay thế
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
74 bằng
55 vàimport requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
76 vàimport argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
77 bằngimport argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
78.Thay thế các chuỗi bằng các đối số ngầm như
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
79 hoặcimport argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
80 bằng cú pháp Python tiêu chuẩn để sử dụng từ điển cho các chuỗi định dạng, nghĩa là,parser.add_argument['filename'] # positional argument parser.add_argument['-c', '--count'] # option that takes a value parser.add_argument['-v', '--verbose', action='store_true'] # on/off flag
80 và
78.import requests from bs4 import BeautifulSoup response = requests.get["//stackoverflow.com/questions"] soup = BeautifulSoup[response.text,"parser.html"] questions = soup.select[".question-summary"]
Thay thế đối số trình xây dựng tùy chọnPapser
import argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
83 bằng một cuộc gọi đếnimport argparse parser = argparse.ArgumentParser[description='Process some integers.'] parser.add_argument['integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator'] parser.add_argument['--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers [default: find the max]'] args = parser.parse_args[] print[args.accumulate[args.integers]]
84.