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: //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: //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
$ 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':'//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
- đượ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
- đầu. Tương tự như nhận yêu cầu nhưng không có phần thân
- Lời 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
import requests url = '//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: //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ầu
import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'//www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json()
đầu ra
{'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': '//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': '//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':'//www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json() 1
import requests url = '//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
- 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 cách sử dụng ký hiệu import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'//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':'//www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json() 3
import requests url = '//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': '//web.archive.org/web/20210930032915///www.jcchouinard.com/', 'timestamp': '20210930032915'}}}
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 import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'//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':'//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 urllib 0
Nhận HTML của trang
$ pip install beautifulsoup4 $ pip install urllib 1
Hiển thị tiêu đề HTTP
$ pip install beautifulsoup4 $ pip install urllib 2
$ pip install beautifulsoup4 $ pip install urllib 3
Hiển thị chuyển hướng
$ pip install beautifulsoup4 $ pip install urllib 4
$ pip install beautifulsoup4 $ pip install urllib 5
Phân tích cú pháp HTML bằng Request và BeautifulSoup
Phân tích cú pháp với BeautifulSoup
$ pip install beautifulsoup4 $ pip install urllib 6
Bạn có thể thấy rằng văn bản khó diễn giải chuỗi
$ pip install beautifulsoup4 $ pip install urllib 7
$ pip install beautifulsoup4 $ pip install urllib 8
$ pip install beautifulsoup4 $ pip install urllib 9
Đầ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':'//www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json() 6 hoặc import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'//www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json() 7
import requests 0
đầu ra
import requests 1
import requests 2
đầu ra
import requests 3
Hoặc, thậm chí chọn các thuộc tính của thẻ
import requests 4
đầu ra
import requests 5
Nhận các thẻ SEO chính từ một trang web
import requests 6
đầu ra
import requests 7
Trích xuất tất cả các liên kết trên một trang
import requests 8
đầu ra
import requests 9
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
import requests url = '//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':'//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 = '//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 = '//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ỗi
import requests url = '//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 = '//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ùng
import requests url = '//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ầu
import requests url = '//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 = '//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 proxy
import requests url = '//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ầu
import requests url = '//crawler-test.com/' response = requests.get(url) print('URL: ', response.url) print('Status code: ', response.status_code) print('HTTP header: ', response.headers) 9
URL: //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: //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: //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: //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 các Yêu cầu import requests payload = { 'name':'Jean-Christophe', 'last_name':'Chouinard', 'website':'//www.jcchouinard.com/' } response = requests.post(url, data = payload) response.json() 9 và {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': '//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': '//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': '//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': '//httpbin.org/post'} 1, {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': '//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': '//httpbin.org/post'} 2, {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': '//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': '//httpbin.org/post'} 3, {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': '//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': '//httpbin.org/post'} 4 và {'args': {}, 'data': '', 'files': {}, 'form': {'last_name': 'Chouinard', 'name': 'Jean-Christophe', 'website': '//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': '//httpbin.org/post'} 5
URL: //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 (bởi 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ộ (của 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 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ận
Nếu bạn đang tìm kiếm một giải pháp thay thế cho thư viện yêu cầu, bạn có thể quan tâm đến thư viện yêu cầu-HTML cung cấp một số tùy chọn phân tích cú pháp HTML tích hợp
Thư viện này không chỉ hữu ích cho việc quét web mà còn cho phát triển web và bất kỳ nỗ lực nào khác sử dụng API
Bây giờ chúng ta kết thúc phần giới thiệu về thư viện Python Requests
5/5 - (4 phiếu)
Jean-Christophe Chouinard
Nhà chiến lược SEO tại Tripadvisor, ex-Seek (Melbourne, Ú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