Lưu ý rằng độc tố không biết khi bạn thay đổi
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
6 và sẽ không tự động cài đặt các phụ thuộc mới để chạy thử nghiệm. Chạy from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
7 để cài đặt một plugin sửa lỗi ngớ ngẩn này Bạn có thể tương tác với đối tượng yêu cầu Python bằng các thuộc tính của nó (e. g.
URL: https://crawler-test.com/
Status code: 200
HTTP header: {'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Sun, 03 Oct 2021 23:41:59 GMT', 'Server': 'nginx/1.10.3', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '8098', 'Connection': 'keep-alive'}
8) và phương pháp (e. g. URL: https://crawler-test.com/
Status code: 200
HTTP header: {'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Sun, 03 Oct 2021 23:41:59 GMT', 'Server': 'nginx/1.10.3', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '8098', 'Connection': 'keep-alive'}
9)Cài đặt yêu cầu Python
Cài đặt phiên bản mới nhất của yêu cầu python bằng pip
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
0Đối với hướng dẫn này, bạn sẽ cần cài đặt Python và cài đặt các gói sau
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
1Nhập mô-đun yêu cầu
Để nhập thư viện
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
20 bằng Python, hãy sử dụng từ khóa nhậpfrom request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
3Phương thức yêu cầu
- được. Yêu cầu dữ liệu
- bưu kiện. Xuất bản dữ liệu
- đặt. Thay thế dữ liệu
- vá. Thực hiện các thay đổi một phần đối với dữ liệu
- xóa bỏ. Xóa dữ liệu
- cái đầu. Tương tự như nhận yêu cầu nhưng không có phần thân
- Yêu cầu. Tạo đối tượng yêu cầu bằng cách chỉ định phương thức để chọn
Nhận yêu cầu
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
4đầu ra
________số 8
Đăng yêu cầu
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
2đầu ra
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
7Phương pháp và thuộc tính phản hồi
Đối tượng phản hồi chứa phản hồi của máy chủ đối với yêu cầu HTTP
Bạn có thể điều tra chi tiết về đối tượng Phản hồi bằng cách sử dụng
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
21from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
9Trong hướng dẫn này, chúng ta sẽ xem xét những điều sau đây
- văn bản, mô tả dữ liệu. Nội dung của phản hồi, trong unicode
- Nội dung, mô tả dữ liệu. Nội dung của phản hồi, tính bằng byte
- url, thuộc tính. URL của yêu cầu
- status_code, thuộc tính. Mã trạng thái được trả về bởi máy chủ
- tiêu đề, thuộc tính. Tiêu đề HTTP được trả về bởi máy chủ
- lịch sử, thuộc tính. danh sách các đối tượng phản hồi nắm giữ lịch sử yêu cầu
- liên kết, thuộc tính. Trả về các liên kết tiêu đề được phân tích cú pháp của phản hồi, nếu có
- json, phương pháp. Trả về nội dung được mã hóa json của phản hồi, nếu có
Truy cập các phương thức và thuộc tính phản hồi
Phản hồi từ yêu cầu là một đối tượng mà bạn có thể truy cập các phương thức và thuộc tính của nó
Bạn có thể truy cập các thuộc tính bằng ký hiệu
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
22 và các phương thức bằng ký hiệu from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
23from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
2from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
3Xử lý phản hồi
Truy cập JSON yêu cầu Python
Trong các yêu cầu Python, phương thức
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
24 cho phép truy cập đối tượng JSON của phản hồi. Nếu kết quả của yêu cầu không được viết ở định dạng JSON, bộ giải mã JSON sẽ trả về ngoại lệ from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
25Hiển thị mã trạng thái
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
10Nhận HTML của trang
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
11Hiển thị tiêu đề HTTP
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
12from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
13Hiển thị chuyển hướng
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
14from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
15Phân tích cú pháp HTML bằng Request và BeautifulSoup
Phân tích cú pháp với BeautifulSoup
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
16Bạn có thể thấy rằng văn bản khó diễn giải chuỗi
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
17from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
18from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
19Đầu ra bây giờ dễ diễn giải hơn khi nó được phân tích cú pháp bằng BeautifulSoup
Bạn có thể trích xuất thẻ bằng phương pháp
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
26 hoặc from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
27from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
30đầu ra
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
31from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
32đầu ra
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
33Hoặc, thậm chí chọn các thuộc tính của thẻ
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
34đầu ra
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
35Nhận các thẻ SEO chính từ một trang web
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
36đầu ra
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
37Trích xuất tất cả các liên kết trên một trang
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
38đầu ra
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
39Cải thiện yêu cầu
Tham số chuỗi truy vấn
Các tham số truy vấn cho phép bạn tùy chỉnh yêu cầu Python của mình bằng cách chuyển các giá trị cho các tham số chuỗi truy vấn. Hầu hết các yêu cầu API đều yêu cầu thêm tham số truy vấn vào yêu cầu. Đây là trường hợp của Wikipedia API
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
40Để thêm tham số chuỗi truy vấn, hãy chuyển từ điển tham số vào đối số
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
28. Đây là cách url yêu cầu trông như thế nàofrom request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
41from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
42Xử lý lỗi
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
43from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
44Thay đổi tác nhân người dùng
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
45Thêm Thời gian chờ để yêu cầu
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
46from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
47Sử dụng proxy
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
48Thêm tiêu đề vào yêu cầu
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
49URL: https://crawler-test.com/
Status code: 200
HTTP header: {'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Sun, 03 Oct 2021 23:41:59 GMT', 'Server': 'nginx/1.10.3', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '8098', 'Connection': 'keep-alive'}
0Phiên yêu cầu
Đối tượng phiên hữu ích khi bạn cần thực hiện các yêu cầu với các tham số tồn tại qua tất cả các yêu cầu trong một phiên duy nhất
URL: https://crawler-test.com/
Status code: 200
HTTP header: {'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Sun, 03 Oct 2021 23:41:59 GMT', 'Server': 'nginx/1.10.3', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '8098', 'Connection': 'keep-alive'}
1URL: https://crawler-test.com/
Status code: 200
HTTP header: {'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Sun, 03 Oct 2021 23:41:59 GMT', 'Server': 'nginx/1.10.3', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '8098', 'Connection': 'keep-alive'}
2Xử lý các lần thử lại trong các yêu cầu Python
URL: https://crawler-test.com/
Status code: 200
HTTP header: {'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Sun, 03 Oct 2021 23:41:59 GMT', 'Server': 'nginx/1.10.3', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '8098', 'Connection': 'keep-alive'}
3Các phương thức HTTP khác
Ngoài Yêu cầu
from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
29 và from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
70, thư viện Python cho phép sử dụng các phương thức HTTP phổ biến khác như from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
71, from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
72, from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
73, from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
74 và from request_session import RequestSession
client = RequestSession(
host="https://jobs.kiwi.com",
max_retries=4, # how many times to retry in case server error occurs
raise_for_status=True, # raise an exception if failed on every attempt
)
response = client.get(
path="/",
sleep_before_repeat=1, # how many seconds to wait untill next try
request_category="jobs", # what to log to stdout/stderr
)
75URL: https://crawler-test.com/
Status code: 200
HTTP header: {'Content-Encoding': 'gzip', 'Content-Type': 'text/html;charset=utf-8', 'Date': 'Sun, 03 Oct 2021 23:41:59 GMT', 'Server': 'nginx/1.10.3', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '8098', 'Connection': 'keep-alive'}
4Thư viện yêu cầu Python là gì?
Thư viện yêu cầu python, còn được gọi là yêu cầu python, là thư viện HTTP cho phép người dùng gửi yêu cầu HTTP bằng Python. Khẩu hiệu của nó “Python HTTP dành cho con người” thể hiện rất rõ sự đơn giản của gói
Hướng dẫn sử dụng Yêu cầu
- API Wikipedia với Python
- Đọc RSS Feed với Python và Beautiful Soup
- Cách đăng trên API LinkedIn bằng Python
- API Reddit không có thông tin xác thực API
- Gửi tin nhắn với API Slack và Python
- Cuộc thi đang sử dụng các danh mục GMB nào?
- Thư viện Python cho SEO – Hướng dẫn cho người mới bắt đầu
- Tác nhân người dùng ngẫu nhiên với Python và BeautifulSoup (của JR Oakes)
- Nhận Điểm BERT cho SEO (của Pierre Rouarch)
Công việc thú vị từ cộng đồng
- Cách kiểm tra mã trạng thái của URL trong Sơ đồ trang web thông qua Python (của Koray Tuğberk GÜBÜR)
- Tự động tìm cơ hội liên kết SEO với Python (của Greg Bernhardt)
- Yoast SEO API Python ví dụ với Yêu cầu + Pandas (của Erick Rumbold)
- Cách tải xuống nhiều hình ảnh trong Python (của James Phoenix)
- Google Autosuggest Trends cho Niche Keywords (bởi Stefan Neefischer)
- Python quét web không đồng bộ (bởi James Phoenix)
Hướng dẫn cạo web khác
- Quét web bằng Python với Scrapy
- Quét web với các yêu cầu-HTML
Sự thật về Yêu cầu Python
Yêu cầu Python Tác giảKenneth Reitz Yêu cầu Python Ngôn ngữPythonYêu cầu Python Phương thứcGET, POST, PUT, DELETE, PATCH, OPTIONS, HEADPython Request Release2011-02-14Phần kết luận
Nếu bạn đang tìm kiếm một giải pháp thay thế cho thư viện yêu cầu, bạn có thể quan tâm đến thư viện yêu cầu-HTML cung cấp một số tùy chọn phân tích cú pháp HTML tích hợp
Thư viện này không chỉ hữu ích cho việc quét web mà còn cho phát triển web và bất kỳ nỗ lực nào khác sử dụng API
Bây giờ chúng ta kết thúc phần giới thiệu về thư viện Python Requests
5/5 - (4 phiếu)
Jean-Christophe Chouinard
Nhà chiến lược SEO tại Tripadvisor, ex-Seek (Melbourne, Australia). Chuyên về kỹ thuật SEO. Trong nhiệm vụ lập trình SEO cho các tổ chức lớn thông qua việc sử dụng Python, R và máy học
Làm cách nào để nhận phiên từ yêu cầu trong Python?
Để lấy đối tượng Phiên trong Yêu cầu Python, bạn cần gọi các yêu cầu. Phương thức phiên() . Đối tượng Phiên có thể lưu trữ các tham số như cookie và tiêu đề HTTP.
Yêu cầu Phiên () trong Python là gì?
Đối tượng phiên cho phép một người duy trì các tham số nhất định trong các yêu cầu . Nó cũng duy trì cookie trên tất cả các yêu cầu được thực hiện từ phiên bản Phiên và sẽ sử dụng tổng hợp kết nối của urllib3.
Yêu cầu Python nhận được gì?
Khi một người gửi yêu cầu tới một URI, nó sẽ trả về một phản hồi . Đối tượng Phản hồi này về mặt python được trả về theo yêu cầu. phương thức (), phương thức là - nhận, đăng, đặt, v.v. Phản hồi là một đối tượng mạnh mẽ với nhiều chức năng và thuộc tính hỗ trợ chuẩn hóa dữ liệu hoặc tạo các phần mã lý tưởng.
Làm thế nào chúng ta có thể nhận được một đối tượng Session?
Đối tượng Phiên được tạo và cung cấp thông qua biến ngữ cảnh, $session. Bạn không cần thực hiện bất kỳ cuộc gọi rõ ràng nào để tạo nó. Bạn có thể lấy một đối tượng Phiên bằng cách sử dụng cú pháp sau, nếu bạn đã có một đối tượng Thực thể hợp lệ. $session=$entity->GetSession();