首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中将Html转换为原始文本

在python中将Html转换为原始文本
EN

Stack Overflow用户
提问于 2019-05-30 17:27:33
回答 1查看 354关注 0票数 0

我已经在Django视图中设置了一个函数,该函数从API中请求数据并通过单击按钮将其保存到数据库中,但是数据的格式有问题。

目标相当简单:从产品描述中删除所有html代码。

下面是描述的当前格式的示例:

代码语言:javascript
复制
<ul>\n  <li>\n  <p>Based on the legendary Roald Dahl’s classic children’s book of the same name, “Matilda! The Musical” is all about little Matilda Wormwood, an extremely smart and bright girl whose genius is unfortunately, unrecognized by her dull parents.</p></li>\n  <li>\n  <p>Meet other characters you will love to hate such as the wicked Mrs. Trunchbull, her evil headmistress and meet Miss Honey, the only one who understands love Matilda with her sharp mind and vivid imagination</p></li>\n  <li>\n  <p>\"Once in a blue moon, a show comes out blazing and restores your faith in Broadway. 'Matilda The Musical' is that show\" <strong>-Elisabeth Vincentelli, NEW YORK POST</strong></p></li>\n  <li>\n  <p>Winner of <strong>5 Tony Awards</strong> and <strong>7 Olivier Awards</strong></p></li>\n</ul>\n<p><strong>FAQ's</strong></p>\n<p>1) <strong>Is there a possibility that I will get partial view seating with my Matilda tickets?</strong></p>\n<p>No, you will only receive full view seats. Although, there are a few seats in the Balcony section that have a thin structural pipe within eyesight. This, however, will not hamper your view or affect your show experience.</p>\n<p>2) <strong>Is there a dress code for Matilda The Musical?</strong></p>\n<p>Smart casual is recommended for Matilda The Musical. Keep in mind the theater is air conditioned throughout the year and can get a bit chilly. </p>\n<p>3) <strong>Is there a minimum age requirement to watch Matilda The Musical?</strong></p>\n<p>All guests require a ticket, regardless of age (for which there is no minimum requirement). We recommend children be at least 6 years of age to attend a performance of Matilda.</p>\n<p>4) <strong>Is the ticket price for children different than adults?</strong></p>\n<p>No, children will have to purchase a full priced ticket.</p>\n<p>5) <strong>Does the Shubert Theater have wheelchair accessible seating?</strong></p>\n<p>Wheelchair accessible seating is available at the Shubert Theater; however, seats are subject to availability. </p>\n<p>6) <strong>What is Matilda The Musical's duration and will there be an intermission?</strong></p>\n<p>The show runs for 2 hours and 30 minutes, with one intermission. </p>\n<p>7) <strong>Can we bring outside food into the Shubert Theater?</strong></p>\n<p>Food and beverages from outside are strictly prohibited.</p>\n<p>8) <strong>Can I can bring a bag, luggage, camera etc.?</strong> </p>\n<p>No large pieces of luggage are allowed inside, but you can check them in at the Secure Baggage Area, located near the entrance. Handbags, cameras, and other small accessories are allowed inside. If carrying a camera, be sure to keep it in your bag at all times. Photography is strictly prohibited inside the theater. </p>\n<p>9) <strong>When should I arrive at the theater?</strong></p>\n<p>We recommend reaching the theater 30 minutes before curtain time. Performances begin promptly at the time stated on your ticket and the theater reserves the right to deny entry if you are late. </p>\n<p>10) <strong>Will my Matilda The Musical seats be together?</strong></p>\n<p>Yes, we always make sure your reservation seats are together. Keep in mind, many theaters use an odd and even-numbered sequence for seating. </p>

预期的结果应该如下所示(删除所有

  • 将其作为原始文本): 根据传奇罗尔德·达尔的传奇儿童书,“玛蒂尔达!“音乐剧”讲述的是小马蒂尔达·沃姆伍德( Matilda ),她是一个非常聪明、聪明的女孩,不幸的是,她的天才没有被她迟钝的父母所认可。亲爱的,她是唯一一个用活泼的头脑和充满想象力的人理解玛蒂尔达的爱的人。“一出戏就能点燃你对百老汇的信心,并恢复你的信心。”纽约邮政5项托尼奖和7项奥利维尔奖得主常见问题1)我能得到我的马蒂尔达门票的部分视图吗?不,你只会收到一张全视野的票。虽然一些座位在阳台部分有一个薄薄的结构管道在视线之内。然而,这不会影响你的视觉或影响你的娱乐体验。2)音乐玛蒂尔达是否有着装规范?音乐马蒂尔达推荐放松的别致。请记住,剧院一年四季都是空调的,可能会有点冷。3)看音乐剧“玛蒂尔达”是否有最低年龄?所有的顾客都需要一张票,不管他们的年龄(这是没有最低要求)。我们建议孩子们至少6岁参加Matilda的表演。4)儿童票价与成人票价是否不同?不,孩子们得买一张全票。5)舒伯特剧院有轮椅无障碍座位吗?舒伯特剧院提供轮椅无障碍座位;但是,座位要视可得性而定。6)音乐玛蒂尔达有多长时间,是否会有一个休息时间?演出持续2小时30分钟,中场休息。7)我们能在舒伯特剧院从外面带来食物吗?严禁外来食品和饮料。我能带包、行李、照相机等吗? 这是我的Django视图中的Def: def api_data(请求):如果request.GET.get('mybtn'):resp_1 = requests.get( "cyCode=CAD",headers={“Headout”:HEADOUT_TEST_API_KEY }) resp_1_data = resp_1.json() base_url_2 = "https://www.test-headout.com/api/public/v1/product/get/“,用于resp_1_data‘item’item:# concat,用于URL = '{}{}'.format(base_url_2 )。#让HTTP请求resp_2 = requests.get( url,description=resp_2_data'contentListHtml''html',headers={“Headout”:HEADOUT_TEST_API_KEY }) resp_2_data = resp_2.json() Product.objects.get_or_create(标题=项目名称,目的地=项目城市,HEADOUT_TEST_API_KEY})#这就是需要格式化的链接=项‘’canonicalUrl‘,image=item'image’)返回呈现(请求,"form.html") 我的Django模型: 类产品(models.Model):目的= models.CharField(max_length=255,默认=‘’)标题= models.CharField(max_length=255,default='')段塞= models.SlugField(null=True,blank=True,unique=True,max_length=255,default='') description = models.TextField(max_length=2000,default='')链接= models.TextField(max_length=500,default='') ptag= TaggableManager()图像= models.ImageField(max_length=500 ),models.DateTimeField(auto_now=True) def get_absolute_url(self):返回反向(‘experience’,kwargs={'slug':self.slug}) def保存(self,*args,**kwargs):如果不是self.id: self.slug = slugify(self.title)超级(Product).save(*args),**kwargs) def __str__(self):返回self.destination 我怎样才能做到这一点? 请帮帮忙。
EN

回答 1

Stack Overflow用户

发布于 2019-05-30 17:30:37

您可以使用lxml或漂亮的汤来解析html。

https://lxml.de/parsing.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56382917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档