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) Show
Cài đặt yêu cầu PythonCài đặt phiên bản mới nhất của yêu cầu python bằng pip $ pip install requests Đố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 $ pip install beautifulsoup4 $ pip install urllib Nhập mô-đun yêu cầuĐể nhập thư viện import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()0 bằng Python, hãy sử dụng từ khóa nhập import requests Phương thức yêu cầu
Nhận yêu cầuimport requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers) đầu ra 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'} Đăng yêu cầuimport requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json() đầu ra {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': 'https://www.jcchouinard.com/'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '85', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.24.0', 'X-Amzn-Trace-Id': 'Root=1-615a4271-417e9fff3c75f47f3af9fde2'}, 'json': None, 'origin': '149.167.130.162', 'url': 'https://httpbin.org/post'} 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 import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()1 import requests url = 'https://crawler-test.com/' response = requests.get(url) help(response) Trong hướng dẫn này, chúng ta sẽ xem xét những điều sau đây
Truy cập các phương thức và thuộc tính phản hồiPhả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 cách sử dụng ký hiệu import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()2 và các phương thức sử dụng ký hiệu import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()3 import requests url = 'http://archive.org/wayback/available?url=jcchouinard.com' response = requests.get(url) response.text # access response data atributes and descriptors response.json() # access response methods {'url': 'jcchouinard.com', 'archived_snapshots': {'closest': {'status': '200', 'available': True, 'url': 'http://web.archive.org/web/20210930032915/https://www.jcchouinard.com/', 'timestamp': '20210930032915'}}} Xử lý phản hồiTruy cập JSON yêu cầu PythonTrong các yêu cầu Python, phương thức import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()4 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ệ import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()5 Hiển thị mã trạng thái$ pip install beautifulsoup4 $ pip install urllib0 Nhận HTML của trang$ pip install beautifulsoup4 $ pip install urllib1 Hiển thị tiêu đề HTTP$ pip install beautifulsoup4 $ pip install urllib2 $ pip install beautifulsoup4 $ pip install urllib3 Hiển thị chuyển hướng$ pip install beautifulsoup4 $ pip install urllib4 $ pip install beautifulsoup4 $ pip install urllib5 Phân tích cú pháp HTML bằng Request và BeautifulSoupPhân tích cú pháp với BeautifulSoup$ pip install beautifulsoup4 $ pip install urllib6 Bạn có thể thấy rằng văn bản khó diễn giải chuỗi $ pip install beautifulsoup4 $ pip install urllib7 $ pip install beautifulsoup4 $ pip install urllib8 $ pip install beautifulsoup4 $ pip install urllib9 Đầ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 cách sử dụng phương pháp import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()6 hoặc import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()7 import requests0 đầu ra import requests1 import requests2 đầu ra import requests3 Hoặc, thậm chí chọn các thuộc tính của thẻ import requests4 đầu ra import requests5 Nhận các thẻ SEO chính từ một trang webimport requests6 đầu ra import requests7 Trích xuất tất cả các liên kết trên một trangimport requests8 đầu ra import requests9 Cải thiện yêu cầuTham số chuỗi truy vấnCá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 import requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)0 Để thêm tham số chuỗi truy vấn, hãy chuyển từ điển tham số vào đối số import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()8. Đây là cách url yêu cầu trông như thế nào import requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)1 import requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)2 Xử lý lỗiimport requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)3 import requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)4 Thay đổi tác nhân người dùngimport requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)5 Thêm Thời gian chờ để yêu cầuimport requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)6 import requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)7 Sử dụng proxyimport requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)8 Thêm tiêu đề vào yêu cầuimport requests url = 'https://crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers)9 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 PythonURL: 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ácNgoài các Yêu cầu import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'https://www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()9 và {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': 'https://www.jcchouinard.com/'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '85', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.24.0', 'X-Amzn-Trace-Id': 'Root=1-615a4271-417e9fff3c75f47f3af9fde2'}, 'json': None, 'origin': '149.167.130.162', 'url': 'https://httpbin.org/post'}0, thư viện Python cho phép sử dụng các phương thức HTTP phổ biến khác như {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': 'https://www.jcchouinard.com/'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '85', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.24.0', 'X-Amzn-Trace-Id': 'Root=1-615a4271-417e9fff3c75f47f3af9fde2'}, 'json': None, 'origin': '149.167.130.162', 'url': 'https://httpbin.org/post'}1, {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': 'https://www.jcchouinard.com/'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '85', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.24.0', 'X-Amzn-Trace-Id': 'Root=1-615a4271-417e9fff3c75f47f3af9fde2'}, 'json': None, 'origin': '149.167.130.162', 'url': 'https://httpbin.org/post'}2, {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': 'https://www.jcchouinard.com/'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '85', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.24.0', 'X-Amzn-Trace-Id': 'Root=1-615a4271-417e9fff3c75f47f3af9fde2'}, 'json': None, 'origin': '149.167.130.162', 'url': 'https://httpbin.org/post'}3, {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': 'https://www.jcchouinard.com/'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '85', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.24.0', 'X-Amzn-Trace-Id': 'Root=1-615a4271-417e9fff3c75f47f3af9fde2'}, 'json': None, 'origin': '149.167.130.162', 'url': 'https://httpbin.org/post'}4 và {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': 'https://www.jcchouinard.com/'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '85', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.24.0', 'X-Amzn-Trace-Id': 'Root=1-615a4271-417e9fff3c75f47f3af9fde2'}, 'json': None, 'origin': '149.167.130.162', 'url': 'https://httpbin.org/post'}5 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
Công việc thú vị từ cộng đồng
Hướng dẫn cạo web khác
Sự thật về Yêu cầu PythonYêu cầu Python Tác giảKenneth ReitzYê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ậnNế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, Úc). 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 Nội dung yêu cầu là gì?Nội dung yêu cầu là dữ liệu do khách hàng gửi tới API của bạn . Nội dung phản hồi là dữ liệu mà API của bạn gửi tới ứng dụng khách. API của bạn hầu như luôn phải gửi nội dung phản hồi. Nhưng khách hàng không nhất thiết phải gửi các cơ quan yêu cầu mọi lúc.
Chúng ta có thể gửi một phần thân trong Python yêu cầu GET không?Có thể chuyển các tiêu đề HTTP bổ sung tới các yêu cầu. phương thức get() với tham số headers=. Bạn không thể gửi dữ liệu trong nội dung của thông báo HTTP GET nhưng vẫn có thể gửi một số thông tin đến máy chủ bằng các tham số URL.
Làm thế nào để sử dụng bài yêu cầu Python?Để gửi yêu cầu POST bằng Thư viện yêu cầu Python, bạn nên gọi yêu cầu. post() và chuyển URL mục tiêu làm tham số đầu tiên và dữ liệu POST với tham số data= .
Gói yêu cầu trong Python là gì?Mô-đun yêu cầu cho phép bạn gửi yêu cầu HTTP bằng Python . Yêu cầu HTTP trả về Đối tượng phản hồi với tất cả dữ liệu phản hồi (nội dung, mã hóa, trạng thái, v.v.). |