Yêu cầu Python nhận được gì?
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 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 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 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 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'}5 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 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 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 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ậ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, 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 Các yêu cầu Python hoạt động như thế nào?Nó hoạt động như một giao thức yêu cầu-phản hồi giữa máy khách và máy chủ. Hãy trình bày cách tạo một yêu cầu GET tới một điểm cuối. Phương thức GET được sử dụng để truy xuất thông tin từ máy chủ nhất định bằng cách sử dụng một URI nhất định . Phương thức GET gửi thông tin người dùng đã mã hóa được thêm vào yêu cầu trang.
Nội dung phản hồi trả về là gì?nội dung trả về nội dung của phản hồi, tính bằng byte . Về cơ bản, nó đề cập đến nội dung Phản hồi nhị phân.
Làm cách nào để nhận phản hồi API bằng Python?Thực hiện lệnh gọi API của bạn . def get_data(bản thân, api) phản ứng = yêu cầu. nhận (f"{api}") nếu phản hồi. status_code == 200 print("lấy dữ liệu thành công") bản thân. formatted_print(phản hồi. json()) print(f"Xin chào mọi người, có {response. status_code} lỗi với yêu cầu của bạn") |