删除文本中的html代码并将其转换为python中的另一种语言

2024-06-01 01:58:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Django视图中设置了一个函数,该函数从API请求数据并将其保存到数据库中,但数据的格式有问题。在

目标很简单:删除所有html代码,并在将描述数据保存到我的数据库之前对其进行翻译。在

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

<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>

预期的结果应该如下所示(删除所有<p><h><ul><li><strong>以获得原始文本并将其翻译成法语):

^{pr2}$

这是我对Django观点的看法:

def api_data(request):
    if request.GET.get('mybtn'):
        resp_1 = requests.get(
            "https://www.headout.com/api/public/v1/product/listing/list-by/city?language=en&cityCode=PARIS&limit=5000&currencyCode=CAD",
            headers={
                "Headout-Auth": HEADOUT_TEST_API_KEY
            })
        resp_1_data = resp_1.json()
        base_url_2 = "https://www.test-headout.com/api/public/v1/product/get/"

        for item in resp_1_data['items']:
            # concat ID to the URL string
            url = '{}{}'.format(base_url_2, item['id'])

            # make the HTTP request
            resp_2 = requests.get(
                url,
                headers={
                    "Headout-Auth": HEADOUT_TEST_API_KEY
                })
            resp_2_data = resp_2.json()

            Product.objects.get_or_create(
                title=item['name'],
                destination=item['city']['name'],
                description=resp_2_data['contentListHtml'][0]['html'], # this is what needs to be formated
                link=item['canonicalUrl'],
                image=item['image']['url']
            )

    return render(request, "form.html")

我的Django模特:

class Product(models.Model):
    destination = models.CharField(max_length=255, default='')
    title = models.CharField(max_length=255, default='')
    slug = models.SlugField(null=True, blank=True, unique=True, max_length=255, default='')
    description = models.TextField(max_length=2000, default='')
    link = models.TextField(max_length=500, default='')

    ptags = TaggableManager()

    image = models.ImageField(max_length=500, default='images/zero-image-found.png')
    timestamp = models.DateTimeField(auto_now=True)

    def get_absolute_url(self):
        return reverse('experience',
                       kwargs={'slug': self.slug})

    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = slugify(self.title)
        super(Product, self).save(*args, **kwargs)

    def __str__(self):
        return self.destination

我怎样才能做到这一点?有没有python模块可以实现呢?在


Tags: andthetoinselfgetismodels