1、富文本框KindEditorphp
一、官網下載:http://kindeditor.net/down.phphtml
放在項目static/kindeditor/目錄下html5
二、html頁面引入kindeditor文件:python
<script src = "/static/kindeditor/kindeditor-all.js"> </script> <script src="/static/js/jquery-3.2.1.min.js"></script> <div> <textarea name="article_content" id="article" cols="30" rows="10"></textarea> </div>
三、js屬性jquery
<script> KindEditor.ready(function(K) { window.editor = K.create('#article',{ // #article,即爲textarea 的id號 width:"900px", height:"500px", uploadJson:"/upload/", items:[ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage', 'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ] extraFileUploadParams:{ "csrfmiddlewaretoken":$("[name='csrfmiddlewaretoken']").val() }, filePostName:"upload_img" }); }); </script>
views.py文件增長上傳文件視圖函數:正則表達式
def upload(request): print(request.FILES) obj=request.FILES.get("upload_img") path=os.path.join(settings.MEDIA_ROOT,"articles",obj.name) with open(path,"wb") as f: for line in obj: f.write(line) res={ "error":0, "url":"/media/articles/"+obj.name } return HttpResponse(json.dumps(res))
四、items說明express
items對應選項圖標說明:json
resizeType:2或1或0,2時能夠拖動改變寬度和高度,1時只能改變高度,0時不能拖動。ide
2、beautifulsoup4模塊函數
Beautiful Soup 4.2.0文檔:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
官方文檔:http://beautifulsoup.readthedocs.io/zh_CN/latest/
一、基本使用:容錯處理,文檔的容錯能力指的是在html代碼不完整的狀況下,使用該模塊能夠識別該錯誤。使用BeautifulSoup解析上述代碼,可以獲得一個 BeautifulSoup 的對象,並能按照標準的縮進格式的結構輸出
##pip3 install beautifulsoup4 #安裝beautifulsoup4 模塊
from bs4 import BeautifulSoup def add_article(request): if request.method=="POST": title=request.POST.get("title") article_content=request.POST.get("article_content") bs=BeautifulSoup(article_content,"html.parser") #html.parser是python自帶的解析器,處理標籤字符串 for tag in bs.find_all(): # 過濾content if tag.name=="script": #查找script標籤 # print(tag.name) tag.decompose() #從文檔樹中刪除 article_content=bs.prettify() #處理好縮進,結構化顯示 desc=bs.text[0:150] #bs.text查找全部的文本內容 obj=Article.objects.create(user=request.user,title=title,desc=desc) ArticleDetail.objects.create(article=obj,content=article_content) return HttpResponse("OK") return render(request,"add_article.html")
二、介紹
下表列出了主要的解析器,以及它們的優缺點,官網推薦使用lxml做爲解析器,由於效率更高. 在Python2.7.3以前的版本和Python3中3.2.2以前的版本,必須安裝lxml或html5lib, 由於那些Python版本的標準庫中內置的HTML解析方法不夠穩定.
三、幾個簡單的瀏覽結構化數據的方法
要處理的html代碼:
html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """
soup.title # <title>The Dormouse's story</title> soup.title.name # u'title' soup.title.string # u'The Dormouse's story' soup.title.parent.name # u'head' soup.p #存在多個相同的標籤則只返回第一個 # <p class="title"><b>The Dormouse's story</b></p> soup.p['class'] # u'title' soup.a #存在多個相同的標籤則只返回第一個 # <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> soup.find_all('a') # [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] soup.find(id="link3") # <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> for link in soup.find_all('a'): #從文檔中找到全部<a>標籤的連接: print(link.get('href')) # http://example.com/elsie # http://example.com/lacie # http://example.com/tillie print(soup.get_text()) #從文檔中獲取全部文字內容: #嵌套選擇 print(soup.head.title.string) print(soup.body.a.string) #子節點、子孫節點 print(soup.p.contents) #p下全部子節點 print(soup.p.children) #獲得一個迭代器,包含p下全部子節點 for i,child in enumerate(soup.p.children): print(i,child) print(soup.p.descendants) #獲取子孫節點,p下全部的標籤都會選擇出來 for i,child in enumerate(soup.p.descendants): print(i,child) #父節點、祖先節點 print(soup.a.parent) #獲取a標籤的父節點 print(soup.a.parents) #找到a標籤全部的祖先節點,父親的父親,父親的父親的父親... #兄弟節點 print('=====>') print(soup.a.next_sibling) #下一個兄弟 print(soup.a.previous_sibling) #上一個兄弟 print(list(soup.a.next_siblings)) #下面的兄弟們=>生成器對象 print(soup.a.previous_siblings) #上面的兄弟們=>生成器對象
四、簡單使用
from bs4 import BeautifulSoup soup = BeautifulSoup(open("index.html")) soup = BeautifulSoup("<html>data</html>")
而後,Beautiful Soup選擇最合適的解析器來解析這段文檔,若是手動指定解析器那麼Beautiful Soup會選擇指定的解析器來解析文檔。
五、搜索文檔數
from bs4 import BeautifulSoup soup= BeautifulSoup(html_doc,"lxml") # 一、字符串:特色:是一種徹底匹配的 print(soup.find_all(name="a")) #找到全部的a標籤 print(soup.find_all(name="a aa")) #找不到,會打印一個[] print(soup.find_all(attrs={"class":"sister"})) print(soup.find_all(text="The Dormouse's story")) #按照文原本找 print(soup.find_all(name="b",text="The Dormouse's story")) #找標籤名是b,而且文本是The Dormouse's story print(soup.p.find(name="b").text) #第一個p標籤的b裏面的文本 print(soup.find_all(name="p",attrs={"class":"story"})) #找到標籤名是p,屬性名是class, print(soup.find(name="p",attrs={"class":"story"}).find_all(name="a")[2]) #找到標籤名是p,屬性名是class的第二個a標籤 # 二、正則 import re print(soup.find_all(name=re.compile("^b"))) #找b開頭的的標籤 print(soup.find_all(attrs={"id":re.compile("link")})) #找到id屬性是link的 print(soup.find_all(text=re.compile(r"\$"))) #找帶有$價錢的文本 # 三、列表:若是傳入列表參數,Beautiful Soup會將與列表中任一元素匹配的內容返回. print(soup.find_all(name=["a",re.compile("^b")])) #找a標籤或者b標籤開頭的全部的標籤 print(soup.find_all(text=["$",])) #找不到 print(soup.find_all(text=[re.compile(r"\$")])) #['$75'] print(soup.find_all(text=["a",re.compile(r"\$")])) # 四、True:能夠匹配任何值 print(soup.find_all(name=True)) #找到全部標籤的標籤名 print(soup.find_all(attrs={"id":True})) #找到只要有id屬性的 print(soup.find_all(name="p",attrs={"id":True})) # 找到有id屬性的p標籤 # 五、方法:若是沒有合適過濾器,那麼還能夠定義一個方法,方法只接受一個元素參數 ,若是這個方法返回 True 表示當前元素匹配而且被找到,若是不是則反回 False # 有class屬性沒有id屬性的 def has_class_not_id(tag): return tag.has_attr('class') and not tag.has_attr('id') # return tag.has_attr('id') and not tag.has_attr('class') # return tag.name =="a" and tag.has_attr("class") and not tag.has_attr("id") # #只找a標籤 print(soup.find_all(has_class_not_id)) #默認是按照標籤來找的 print(soup.find_all(name="a",limit=2)) #找全部的a標籤,只找前兩個 print(soup.body.find_all(attrs={"class":"sister"},recursive=False))#找屬性爲sister的 print(soup.html.find_all('a')) print(soup.html.find_all('a',recursive=False)) # recursive = True #從子子孫孫都找到了 # recursive = False #若是隻想搜索tag的直接子節點(就不往裏面找了),可使用參數 recursive=False . # **kwargs print(soup.find_all(attrs={"class":"sister"})) print(soup.find_all(class_="sister")) #這兩個是同樣的 print(soup.find_all(attrs={"id":"link3"})) #這兩個是同樣的,只是表示方式不同 print(soup.find_all(id="link3"))
六、find_all( name , attrs , recursive , text , **kwargs )
#2.一、name: 搜索name參數的值可使任一類型的 過濾器 ,字符竄,正則表達式,列表,方法或是 True . print(soup.find_all(name=re.compile('^t'))) #2.二、keyword: key=value的形式,value能夠是過濾器:字符串 , 正則表達式 , 列表, True . print(soup.find_all(id=re.compile('my'))) print(soup.find_all(href=re.compile('lacie'),id=re.compile('\d'))) #注意類要用class_ print(soup.find_all(id=True)) #查找有id屬性的標籤 # 有些tag屬性在搜索不能使用,好比HTML5中的 data-* 屬性: data_soup = BeautifulSoup('<div data-foo="value">foo!</div>','lxml') # data_soup.find_all(data-foo="value") #報錯:SyntaxError: keyword can't be an expression # 可是能夠經過 find_all() 方法的 attrs 參數定義一個字典參數來搜索包含特殊屬性的tag: print(data_soup.find_all(attrs={"data-foo": "value"})) # [<div data-foo="value">foo!</div>] #2.三、按照類名查找,注意關鍵字是class_,class_=value,value能夠是五種選擇器之一 print(soup.find_all('a',class_='sister')) #查找類爲sister的a標籤 print(soup.find_all('a',class_='sister ssss')) #查找類爲sister和sss的a標籤,順序錯誤也匹配不成功 print(soup.find_all(class_=re.compile('^sis'))) #查找類爲sister的全部標籤 #2.四、attrs print(soup.find_all('p',attrs={'class':'story'})) #2.五、text: 值能夠是:字符,列表,True,正則 print(soup.find_all(text='Elsie')) print(soup.find_all('a',text='Elsie')) #2.六、limit參數:若是文檔樹很大那麼搜索會很慢.若是咱們不須要所有結果,可使用 limit 參數限制返回結果的數量.效果與SQL中的limit關鍵字相似,當搜索到的結果數量達到 limit 的限制時,就中止搜索返回結果 print(soup.find_all('a',limit=2)) #2.七、recursive:調用tag的 find_all() 方法時,Beautiful Soup會檢索當前tag的全部子孫節點,若是隻想搜索tag的直接子節點,可使用參數 recursive=False . print(soup.html.find_all('a')) print(soup.html.find_all('a',recursive=False)) ''' 像調用 find_all() 同樣調用tag find_all() 幾乎是Beautiful Soup中最經常使用的搜索方法,因此咱們定義了它的簡寫方法. BeautifulSoup 對象和 tag 對象能夠被看成一個方法來使用,這個方法的執行結果與調用這個對象的 find_all() 方法相同,下面兩行代碼是等價的: soup.find_all("a") soup("a") 這兩行代碼也是等價的: soup.title.find_all(text=True) soup.title(text=True) ''' 三、find( name , attrs , recursive , text , **kwargs ) find_all() 方法將返回文檔中符合條件的全部tag,儘管有時候咱們只想獲得一個結果.好比文檔中只有一個<body>標籤,那麼使用 find_all() 方法來查找<body>標籤就不太合適, 使用 find_all 方法並設置 limit=1 參數不如直接使用 find() 方法.下面兩行代碼是等價的: soup.find_all('title', limit=1) # [<title>The Dormouse's story</title>] soup.find('title') # <title>The Dormouse's story</title> 惟一的區別是 find_all() 方法的返回結果是值包含一個元素的列表,而 find() 方法直接返回結果. find_all() 方法沒有找到目標是返回空列表, find() 方法找不到目標時,返回 None . print(soup.find("nosuchtag")) # None soup.head.title 是 tag的名字 方法的簡寫.這個簡寫的原理就是屢次調用當前tag的 find() 方法: soup.head.title # <title>The Dormouse's story</title> soup.find("head").find("title") # <title>The Dormouse's story</title>