Yêu cầu Python Phiên nhậ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
)
1 là một thư viện HTTP được xây dựng dựa trê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
)
2 giúp cuộc sống của bạn dễ dàng hơn bằng cách thử lại bất cứ khi nào yêu cầu không thành công, ghi lại kết quả hoặc thậm chí gửi chỉ số và dấu vết tới DataDogHQ. RequestSession cũng đo thời gian của yêu cầu

Show

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
)
3 để tạo mô-đun máy khách để truy cập một nhóm tài nguyên được cung cấp trên một điểm cuối chung với các giá trị mặc định hợp lệ cho tất cả hoặc hầu hết các cuộc gọi mà bạn cần thực hiện. Để thuận tiện cho bạn, một số giá trị mặc định đã được cung cấp

Cách sử dụng

Để cài đặt

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
)
1, hãy sử dụng pip

pip install request-session
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
)

Lợi ích của việc 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 ) 3

  • Thử lại. Có thể cấu hình thử lại trong trường hợp yêu cầu thất bại
  • Nhật ký. Kết quả của yêu cầu cũng có thể được ghi vào
    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
  • số liệu. Trên mỗi cuộc gọi bạn thực hiện với các 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
    )
    
    7,
    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
    )
    
    8,
    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,
    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
    )
    
    1 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
    )
    
    2, một số chỉ số được gửi đến ứng dụng khách datadog của bạn - thời lượng của yêu cầu, số lượng yêu cầu đã được gửi, kết quả của yêu cầu (cần tích hợp DataDog)
  • truy tìm.
    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
    )
    
    3 có thể gửi thông tin theo dõi tới DataDog (cần tích hợp APM)

Bạn có thể tìm thêm chi tiết về các lợi ích và ví dụ của

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
)
3 trong tài liệu chính thức

Làm thế nào để chạy thử nghiệm

Để chạy tất cả các bài kiểm tra, bạn chỉ cần chạy lệ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
)
5

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
)
1

Nhậ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ậ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
)
3

Phươ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
)
7

Phươ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
)
21

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
)
9

Trong 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
)
23

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

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
)
3

Xử 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
)
25

Hiể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
)
10

Nhậ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
)
11

Hiể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
)
12

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
)
13

Hiể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
)
14

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
)
15

Phâ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
)
16

Bạ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
)
17

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
)
18

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
)
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
)
27

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
)
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
)
31

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
)
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
)
33

Hoặ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
)
35

Nhậ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
)
37

Trí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
)
39

Cả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ào

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
)
41

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
)
42

Xử 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
)
43

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
)
44

Thay đổ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
)
45

Thê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
)
46

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
)
47

Sử 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
)
48

Thê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
)
49

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'}
0

Phiê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'}
1

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'}
2

Xử 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'}
3

Cá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
)
75

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'}
4

Thư 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-14

Phầ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)

Yêu cầu Python Phiên nhận

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();