Phiên bản ngắn!
import re, html
tag_re = re.compile[r'[|]*>]']
# Remove well-formed tags, fixing mistakes by legitimate users
no_tags = tag_re.sub['', user_input]
# Clean up anything else by escaping
ready_for_web = html.escape[no_tags]
REGEX Nguồn: MarkupSafe. Phiên bản của họ cũng xử lý các thực thể HTML, trong khi phiên bản nhanh này thì không.
Tại sao tôi không thể tước các thẻ và để nó?
Đó là một điều để giữ cho mọi người khỏi italicizing
, mà không rời khỏi i
nổi xung quanh. Nhưng đó là một thứ khác để lấy đầu vào tùy ý và làm cho nó hoàn toàn vô hại. Hầu hết các kỹ thuật trên trang này sẽ để lại những thứ như bình luận chưa được giải thích [ src=x onerror=alert[1];//>
0 và
src=x onerror=alert[1];//
1 sẽ được mọi người thoát y trên trang này cho phép [ngoại trừ @medeiros!], Bởi vì chúng không hoàn thành thẻ. Tổ chức các thẻ HTML bình thường là không đủ.
Django's
src=x onerror=alert[1];//
2, phiên bản cải tiến [xem tiêu đề tiếp theo] của câu trả lời hàng đầu cho câu hỏi này, đưa ra cảnh báo sau:Hoàn toàn không có gì đảm bảo được cung cấp về chuỗi kết quả là HTML an toàn. Vì vậy, không bao giờ đánh dấu an toàn kết quả của một cuộc gọi
2 mà không thoát khỏi nó trước, ví dụ vớisrc=x onerror=alert[1];//
4.src=x onerror=alert[1];//
Làm theo lời khuyên của họ!
Để dải thẻ với htmlparser, bạn phải chạy nó nhiều lần.
Thật dễ dàng để phá vỡ câu trả lời hàng đầu cho câu hỏi này.
Nhìn vào chuỗi này [nguồn và thảo luận]:
src=x onerror=alert[1];//
Lần đầu tiên HTMLPARSER nhìn thấy nó, nó không thể nói rằng
src=x onerror=alert[1];//
5 là một thẻ. Nó trông bị hỏng, vì vậy htmlparser không thoát khỏi nó. Nó chỉ lấy ra src=x onerror=alert[1];//
6, để lại cho bạn
Vấn đề này đã được tiết lộ cho dự án Django vào tháng 3 năm 2014.
src=x onerror=alert[1];//
2 cũ của họ về cơ bản giống như câu trả lời hàng đầu cho câu hỏi này. Phiên bản mới của họ về cơ bản chạy nó trong một vòng lặp cho đến khi chạy lại nó không thay đổi chuỗi:# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
Tất nhiên, không có vấn đề nào trong số này nếu bạn luôn thoát khỏi kết quả của
src=x onerror=alert[1];//
8.Cập nhật ngày 19 tháng 3 năm 2015: Có một lỗi trong các phiên bản Django trước 1.4.20, 1.6.11, 1.7.7 và 1.8C1. Các phiên bản này có thể nhập một vòng lặp vô hạn trong hàm dải_tags []. Phiên bản cố định được sao chép ở trên. Thêm chi tiết ở đây.: There was a bug in Django versions before 1.4.20, 1.6.11, 1.7.7, and 1.8c1. These versions could enter an infinite loop in the strip_tags[] function. The fixed version is reproduced above. More details here.
Những điều tốt để sao chép hoặc sử dụng
Mã ví dụ của tôi không xử lý các thực thể HTML - các phiên bản đóng gói Django và MarkupSafe.
Mã ví dụ của tôi được lấy từ thư viện MarkupSafe tuyệt vời để phòng ngừa kịch bản chéo trang. Nó thuận tiện và nhanh chóng [với tốc độ C đến phiên bản Python gốc của nó]. Nó được bao gồm trong Google App Engine và được sử dụng bởi Jinja2 [2.7 trở lên], Mako, Pylons, v.v. Nó hoạt động dễ dàng với các mẫu Django từ Django 1.7.
Django's Strip_tags và các tiện ích HTML khác từ một phiên bản gần đây là tốt, nhưng tôi thấy chúng ít thuận tiện hơn so với MarkupSafe. Chúng khá khép kín, bạn có thể sao chép những gì bạn cần từ tệp này.
Nếu bạn cần loại bỏ hầu hết tất cả các thẻ, thư viện Bleach là tốt. Bạn có thể thực thi các quy tắc như "Người dùng của tôi có thể in nghiêng mọi thứ, nhưng họ không thể tạo ra iframe."
Hiểu các thuộc tính của vũ nữ thoát y thẻ của bạn! Chạy các bài kiểm tra fuzz trên đó! Đây là mã tôi đã sử dụng để thực hiện nghiên cứu cho câu trả lời này.
Lưu ý ngượng ngùng - Bản thân câu hỏi là về việc in vào bảng điều khiển, nhưng đây là kết quả hàng đầu của Google cho "Dải Python HTML từ chuỗi", vì vậy đó là lý do tại sao câu trả lời này là 99% về web.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc html module
Bàn luận+], we can convert the ASCII string into HTML script by replacing ASCII characters with special characters by using html.escape[]method.
Điều kiện tiên quyết: Mô -đun HTML
Syntax:
html.unescape[String]
Đưa ra một chuỗi có các ký tự HTML, tác vụ là chuyển đổi các ký tự HTML thành một chuỗi. Điều này có thể đạt được với sự trợ giúp của phương thức html.escape [] [đối với Python 3.4+], chúng ta có thể chuyển đổi chuỗi ASCII thành tập lệnh HTML bằng cách thay thế các ký tự ASCII bằng các ký tự đặc biệt bằng cách sử dụng phương thức HTML.ESCAPE [].which handles entity conversion. In Beautiful Soup 4, entities get decoded automatically.
Bằng phương pháp này, chúng ta có thể giải mã các thực thể HTML thành văn bản. Python 3.6+
Python3
Chúng tôi cũng có thể sử dụng Soupwhich đẹp xử lý chuyển đổi thực thể. Trong súp đẹp 4, các thực thể được giải mã tự động.
Ví dụ 1: Python 3.6+
4
5
4
7Output:
Γeek cho γeek
9src=x onerror=alert[1];//
0
1
2
3Python 2.6-3.3
& Gamma; eek for & gamma; eek
- Ví dụ 2: Python 2.6-3.3
- Chúng ta có thể sử dụng htmlparser.Unescape [] từ thư viện tiêu chuẩn:
Python3
Chúng tôi cũng có thể sử dụng Soupwhich đẹp xử lý chuyển đổi thực thể. Trong súp đẹp 4, các thực thể được giải mã tự động.
# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
0# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
1# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
2# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
3 # _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
4 src=x onerror=alert[1];//
9 # _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
6# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
7 # _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
8# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
2# _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
3 html.unescape[String]1
src=x onerror=alert[1];//
9 # _strip_once runs HTMLParser once, pulling out just the text of all the nodes.
def strip_tags[value]:
"""Returns the given HTML with all tags stripped."""
# Note: in typical case this loop executes _strip_once once. Loop condition
# is redundant, but helps to reduce number of executions of _strip_once.
while '' in value:
new_value = _strip_once[value]
if len[new_value] >= len[value]:
# _strip_once was not able to detect more tags
break
value = new_value
return value
6html.unescape[String]4
2 html.unescape[String]6
4html.unescape[String]8
3italicizing
0Output:
Γeek cho γeek