Yêu cầu Python Tác nhân người dùng

user_agents là một thư viện Python cung cấp một cách dễ dàng để xác định/phát hiện các thiết bị như điện thoại di động, máy tính bảng và khả năng của chúng bằng cách phân tích chuỗi tác nhân người dùng (trình duyệt/HTTP). Mục đích là để phát hiện một cách đáng tin cậy liệu

  • Tác nhân người dùng là thiết bị di động, máy tính bảng hoặc PC
  • Tác nhân người dùng có khả năng cảm ứng (có màn hình cảm ứng)

user_agents dựa vào trình phân tích cú pháp ua tuyệt vời để thực hiện phân tích cú pháp thực tế của chuỗi tác nhân người dùng thô

Cài đặt

user-agents được lưu trữ trên PyPI và có thể được cài đặt như vậy

pip install pyyaml ua-parser user-agents

Ngoài ra, bạn cũng có thể lấy mã nguồn mới nhất từ ​​Github và cài đặt thủ công

Cách sử dụng

Các thông tin cơ bản khác nhau có thể giúp bạn xác định khách truy cập có thể được truy cập các thuộc tính browser, deviceos. Ví dụ

from user_agents import parse

# iPhone's user agent string
ua_string = 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3'
user_agent = parse(ua_string)

# Accessing user agent's browser attributes
user_agent.browser  # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')
user_agent.browser.family  # returns 'Mobile Safari'
user_agent.browser.version  # returns (5, 1)
user_agent.browser.version_string   # returns '5.1'

# Accessing user agent's operating system properties
user_agent.os  # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')
user_agent.os.family  # returns 'iOS'
user_agent.os.version  # returns (5, 1)
user_agent.os.version_string  # returns '5.1'

# Accessing user agent's device properties
user_agent.device  # returns Device(family=u'iPhone', brand=u'Apple', model=u'iPhone')
user_agent.device.family  # returns 'iPhone'
user_agent.device.brand # returns 'Apple'
user_agent.device.model # returns 'iPhone'

# Viewing a pretty string version
str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1"

user_agents cũng phơi bày một vài thuộc tính "phức tạp" khác có nguồn gốc từ một hoặc nhiều thuộc tính cơ bản được xác định ở trên. Hiện tại, các thuộc tính này sẽ xác định chính xác các nền tảng/thiết bị phổ biến, các yêu cầu kéo để hỗ trợ các nền tảng/thiết bị nhỏ hơn luôn được chào đón

Bài viết này giải thích chủ đề về tác nhân người dùng tiêu đề HTTP và cách đặt tác nhân người dùng bằng cách sử dụng

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 trong Python. Bạn sẽ tìm hiểu về các tiêu đề HTTP và tầm quan trọng của chúng trong việc hiểu tác nhân người dùng, nhận tác nhân người dùng và tìm hiểu nhiều cách để đặt tác nhân người dùng bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 trong Python

Hiểu cách hoạt động của tiêu đề HTTP

Mọi yêu cầu và phản hồi HTTP bao gồm một chuỗi các chuỗi được gọi là trường tiêu đề HTTP được gửi và nhận bởi cả ứng dụng khách và máy chủ. Tiêu đề HTTP cho phép truyền thông tin bổ sung đến máy chủ và để máy chủ gửi lại thông tin đó

Làm việc với các tiêu đề cho phép những việc như xác thực chính bạn khi sử dụng API hoặc truyền đạt yêu cầu về loại thông tin mà ứng dụng mong đợi

Hãy xem xét một số ví dụ về tiêu đề HTTP

  1. Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
    Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
    Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
    
    6. Trường tiêu đề chung
    Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
    Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
    Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
    
    6 thể hiện các hướng dẫn mà TẤT CẢ các hệ thống bộ nhớ đệm PHẢI tuân theo. Tiêu đề chung
    Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
    Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
    Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
    
    6 có thể được sử dụng bởi máy khách hoặc máy chủ HTTP để xác định các tham số bộ đệm hoặc để thực hiện các yêu cầu bộ đệm cụ thể cho các loại tài liệu cụ thể

    Một danh sách các chỉ thị bộ nhớ đệm được phân tách bằng dấu phẩy được cung cấp. Ví dụ

    Cache-Control : cache-request-directive|cache-response-directive
    # OR
    Cache-control: no-cache
    

  2. Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
    Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
    Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
    
    9. Không có ngoại lệ, mọi dấu ngày/giờ HTTP phải luôn được thể hiện bằng Giờ trung bình Greenwich (GMT). Bất kỳ một trong ba biểu diễn tem ngày/giờ sau đây đều có thể được sử dụng bởi các ứng dụng HTTP

    Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
    Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
    Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
    

  3. import requests
    
    url = 'http://www.tutorialspoint.com/python'
    
    headers = requests.get(url).headers
    print(headers)
    
    0. Nó trả về thông tin như phiên bản trình duyệt và HĐH hệ thống của máy tính mà yêu cầu được gửi. Đây là tiêu đề sẽ được tập trung cụ thể trong bài viết này, giải thích cách đặt tác nhân người dùng bằng cách sử dụng
    Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
    Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
    Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
    
    4

    User-Agent Mozilla

Trường hợp của các tiêu đề này là không liên quan vì các tiêu đề này không phân biệt chữ hoa chữ thường, do đó, cụm từ

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
2 có thể được viết thay thế là
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3

Hãy xem một ví dụ về cách in tiêu đề từ một URL. Chương trình này gửi các yêu cầu đến URL được nhắm mục tiêu, tìm nạp tất cả các tiêu đề từ đó và sau đó in chúng

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)

Dòng mã đầu tiên nhập gói thư viện Python

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4. Một biến đối tượng
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
5 được tạo để lưu trữ URL của trang web mà yêu cầu sẽ được gửi đến

Bên trong biến

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
6, hàm
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
7 được sử dụng để lấy các tiêu đề từ
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
5 và sau đó được lưu bên trong biến. Cuối cùng, biến
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
6 được in ra

Điều này in toàn bộ từ điển tiêu đề từ URL đã cho

đầu ra

C:\python38\python.exe "C:/Users/Win 10/main.py"
{'Content-Encoding': 'gzip', 'Age': '1490828', 'Cache-Control': 'max-age=2592000', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Sat, 24 Sep 2022 17:50:06 GMT', 'Expires': 'Mon, 24 Oct 2022 17:50:06 GMT', 'Last-Modified': 'Wed, 07 Sep 2022 11:42:58 GMT', 'Server': 'ECAcc (tir/CD76)', 'Strict-Transport-Security': 'max-age=63072000; includeSubdomains', 'Vary': 'Accept-Encoding', 'X-Cache': 'HIT', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-Version': 'June-1 V2', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '9299'}

Process finished with exit code 0

Nhận dữ liệu tác nhân người dùng bằng Python

Chúng tôi đã thấy cách lấy toàn bộ từ điển tiêu đề bằng Python. Trước khi học cách đặt

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4, điều quan trọng là phải hiểu phần này

Phần này giải thích một điểm nối quan trọng gọi cụ thể là

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 trong số tất cả các tiêu đề từ URL và hiển thị nó

Khi tác nhân người dùng trả về dữ liệu của trình duyệt và hệ điều hành, nó phụ thuộc rất nhiều vào phương thức được sử dụng để truy cập trang web. Ví dụ: trang web

C:\python38\python.exe "C:/Users/Win 10/main.py"
{'Content-Encoding': 'gzip', 'Age': '1490828', 'Cache-Control': 'max-age=2592000', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Sat, 24 Sep 2022 17:50:06 GMT', 'Expires': 'Mon, 24 Oct 2022 17:50:06 GMT', 'Last-Modified': 'Wed, 07 Sep 2022 11:42:58 GMT', 'Server': 'ECAcc (tir/CD76)', 'Strict-Transport-Security': 'max-age=63072000; includeSubdomains', 'Vary': 'Accept-Encoding', 'X-Cache': 'HIT', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'X-Version': 'June-1 V2', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '9299'}

Process finished with exit code 0
3 trả về dữ liệu tác nhân người dùng cụ thể khi được truy cập bằng các trình duyệt khác nhau

Khi Firefox được sử dụng để truy cập URL này, tác nhân người dùng được đặt là

User-Agent Mozilla

Khi Google Chrome được sử dụng, tác nhân người dùng sẽ thay đổi theo nó

User-Agent Chrome

Nhưng khi tập lệnh Python sử dụng hàm

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 được thực thi để lấy
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3, thì URL sẽ trả về phiên bản của yêu cầu

Ở đây trong chương trình bên dưới, hàm

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 được sử dụng để tìm nạp dữ liệu tác nhân người dùng hiển thị kết quả khác với kết quả đã thấy với trình duyệt

import requests

r = requests.get('http://httpbin.org/user-agent')

data = r.json()
print(data['user-agent'])

URL trả về phiên bản được yêu cầu của tập lệnh Python được sử dụng

đầu ra

C:\python38\python.exe "C:/Users/Win 10/main.py"
python-requests/2.28.1

Process finished with exit code 0

Đặt giá trị tác nhân người dùng bằng cách sử dụng các yêu cầu trong Python

Phần này sẽ giải thích cách thêm tiêu đề tùy chỉnh và đặt

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 trong Python. Phần này trình bày bốn phương pháp khác nhau để đặt
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4

Đặt Yêu cầu Tác nhân Người dùng trong Python cho Yêu cầu Phiên bản 2. 12

Nếu hệ thống chạy phiên bản python chạy phiên bản

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 2. 12 tuổi trở lên thì nên theo phương pháp này

Chương trình đầu tiên cho thấy cách lấy các tiêu đề mặc định và sau đó cập nhật cụ thể để đặt

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4

Nhập gói thư viện

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 và đặt URL trong một biến đối tượng. Trong biến
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
6, lưu trữ bản sao của các tiêu đề mặc định mà gói thư viện
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 sẽ sử dụng

Thay thế các tiêu đề mặc định bằng tiêu đề tùy chỉnh dự định. Bởi vì mã nguồn của yêu cầu có cách triển khai

import requests

r = requests.get('http://httpbin.org/user-agent')

data = r.json()
print(data['user-agent'])
7 duy nhất cho các tiêu đề mặc định, từ điển chuyển sang phân biệt chữ hoa chữ thường

Tạo một biến

import requests

r = requests.get('http://httpbin.org/user-agent')

data = r.json()
print(data['user-agent'])
8 nhắm mục tiêu URL bằng yêu cầu nhận và tìm nạp trạng thái phản hồi của URL. Nếu URL đang hoạt động và đang chạy, nó sẽ trả về mã -
import requests

r = requests.get('http://httpbin.org/user-agent')

data = r.json()
print(data['user-agent'])
9, mã này sẽ được in ra

Do tiêu đề tùy chỉnh được đặt cho

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3, nên dữ liệu bên trong
import requests

r = requests.get('http://httpbin.org/user-agent')

data = r.json()
print(data['user-agent'])
8 phải được chuyển đổi thành JSON để có thể truy cập cụ thể các tiêu đề như tác nhân người dùng

Dữ liệu JSON của biến

import requests

r = requests.get('http://httpbin.org/user-agent')

data = r.json()
print(data['user-agent'])
8 được lưu trữ bên trong biến
C:\python38\python.exe "C:/Users/Win 10/main.py"
python-requests/2.28.1

Process finished with exit code 0
3. Chương trình tìm kiếm một
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 từ dữ liệu và tác nhân người dùng mới được đặt bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 được in

import requests

url = 'http://httpbin.org/user-agent'

headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'My User Agent 1.0',
    }
)

response = requests.get(url, headers=headers)
print(response)

data = response.json()
print(data['user-agent'])

đầu ra

C:\python38\python.exe "C:/Users/Win 10/main.py"

My User Agent 1.0

Process finished with exit code 0

Đặt Yêu cầu Tác nhân Người dùng bằng Python cho Yêu cầu Phiên bản 2. 13

Phiên bản mới hơn của

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 cho phép cập nhật tiêu đề trực tiếp bằng cách tạo từ điển. Trong ví dụ này, chương trình đặt hai tiêu đề -
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 và
C:\python38\python.exe "C:/Users/Win 10/main.py"
python-requests/2.28.1

Process finished with exit code 0
8

Cả hai đều là tiêu đề HTTP hợp lệ được cập nhật

Giá trị của

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
7 được lưu bên trong biến
import requests

r = requests.get('http://httpbin.org/user-agent')

data = r.json()
print(data['user-agent'])
8, tương tự như chương trình trước. Để kiểm tra xem tiêu đề HTTP
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 có được cập nhật đúng cách hay không, hàm
import requests

url = 'http://httpbin.org/user-agent'

headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'My User Agent 1.0',
    }
)

response = requests.get(url, headers=headers)
print(response)

data = response.json()
print(data['user-agent'])
2 được sử dụng;

Bên trong biến

import requests

url = 'http://httpbin.org/user-agent'

headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'My User Agent 1.0',
    }
)

response = requests.get(url, headers=headers)
print(response)

data = response.json()
print(data['user-agent'])
4, giá trị mặc định
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 được lưu bằng hàm
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4. Sử dụng cú pháp
import requests

url = 'http://httpbin.org/user-agent'

headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'My User Agent 1.0',
    }
)

response = requests.get(url, headers=headers)
print(response)

data = response.json()
print(data['user-agent'])
7, giá trị được in

Các tiêu đề mà chúng tôi đã gửi được truy cập bởi hàm

import requests

url = 'http://httpbin.org/user-agent'

headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'My User Agent 1.0',
    }
)

response = requests.get(url, headers=headers)
print(response)

data = response.json()
print(data['user-agent'])
8, do đó, tiêu đề
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 cập nhật mới được lưu bên trong biến
C:\python38\python.exe "C:/Users/Win 10/main.py"

My User Agent 1.0

Process finished with exit code 0
0 bằng cách sử dụng cú pháp
C:\python38\python.exe "C:/Users/Win 10/main.py"

My User Agent 1.0

Process finished with exit code 0
1, sau đó nó được in ra

Một biến mới,

C:\python38\python.exe "C:/Users/Win 10/main.py"

My User Agent 1.0

Process finished with exit code 0
2, được tạo để kiểm tra tất cả các tiêu đề, bao gồm cả những tiêu đề được cập nhật. Nó lại sử dụng hàm
import requests

url = 'http://httpbin.org/user-agent'

headers = requests.utils.default_headers()
headers.update(
    {
        'User-Agent': 'My User Agent 1.0',
    }
)

response = requests.get(url, headers=headers)
print(response)

data = response.json()
print(data['user-agent'])
8 để lưu trữ các tiêu đề bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4

import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)

đầu ra

C:\python38\python.exe "C:/Users/Win 10/main.py"

Default user-agent header =  python-requests/2.28.1
Updated user-agent header =  My User Agent 1.0
{'User-Agent': 'My User Agent 1.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'From': '[email protected]'}

Process finished with exit code 0

Một phương pháp khác để đặt

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 là gửi các yêu cầu giống như một trình duyệt. Trong ví dụ dưới đây, bên trong biến
C:\python38\python.exe "C:/Users/Win 10/main.py"

My User Agent 1.0

Process finished with exit code 0
7, trường
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 được cập nhật với loại dữ liệu trình duyệt

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
0

đầu ra

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
1

Một phương pháp đơn giản khác để đặt

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 là sử dụng mô-đun
import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
1, loại bỏ nhu cầu đặt tiêu đề nhiều lần

Biến đối tượng

import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
1 sử dụng hàm
import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
3 để tải phiên URL. Nó tương tự như các yêu cầu
import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
4 mà các chương trình trước đó đã sử dụng

Tiêu đề

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 mới được cập nhật thông qua chức năng
import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
6

Sau đó, hàm

import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
7 tải thông tin tiêu đề từ URL. Cuối cùng, cú pháp
import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
8 được in ra để lấy thông tin cập nhật của tập hợp
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
2

Sau khi chương trình đặt

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 và
import requests

url = 'https://httpbin.org/get'

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'
}

response = requests.get(url, headers=headers)
print(response)

previous_agent = requests.utils.default_user_agent()
print("Default user-agent header = ", previous_agent)

updated_agent = response.request.headers['user-agent']
print("Updated user-agent header = ", updated_agent)

all_headers = response.request.headers
print(all_headers)
1, chương trình sẽ in tiêu đề đã cập nhật được tải bên trong phiên

đầu ra

Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
3

Bài viết này đã giải thích cặn kẽ cách đặt

import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 và cách lấy chúng. Bạn đọc có thể dễ dàng thiết lập
import requests

url = 'http://www.tutorialspoint.com/python'

headers = requests.get(url).headers
print(headers)
3 bằng cách sử dụng
Sat, 22 Sep 2022 20:22:37 GMT  ; RFC 822, updated by RFC 1123
Saturday, 22-Sep-22 20:23:37 GMT ; RFC 850, obsoleted by RFC 1036
Sat Sep 22 20:24:37 2022       ; ANSI C's asctime() format
4 trong Python sau khi xem qua bài viết

Tác nhân người dùng yêu cầu Python là gì?

user_agents là thư viện Python cung cấp một cách dễ dàng để xác định/phát hiện các thiết bị như điện thoại di động, máy tính bảng và khả năng của chúng bằng cách phân tích chuỗi tác nhân người dùng (trình duyệt/HTTP). The goal is to reliably detect whether: User agent is a mobile, tablet or PC based device.

Làm cách nào để nhận tác nhân người dùng từ yêu cầu trong Python?

Ví dụ về mã .
Khởi tạo. từ getuseragent nhập UserAgent useragent = UserAgent() theuseragent = useragent. .
Tác nhân người dùng có sẵn. Mỗi danh sách sau đây chứa 100 tác nhân người dùng thường được sử dụng cho phần mềm/thiết bị/hệ thống đó. .
Sự kết hợp. .
In tác nhân người dùng ngẫu nhiên từ danh sách. .
Hiệu suất / Hạn chế. .
Trình xử lý yêu cầu

Tác nhân người dùng yêu cầu là gì?

Tiêu đề yêu cầu Tác nhân người dùng là một chuỗi đặc trưng cho phép các máy chủ và mạng ngang hàng xác định ứng dụng, hệ điều hành, nhà cung cấp và/hoặc phiên bản của tác nhân người dùng yêu cầu.