找不到使用beautiful soup和python的xml标记

2024-09-27 22:42:35 发布

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

我想把图片:标题标签包含来自xml页面的特定关键字。如果我只是在loc标签上搜索,关键字就可以正常工作。以下代码

print("Searching for product...")
        keywordLinkFound = False
        while keywordLinkFound is False:
            html = self.driver.page_source
            soup = BeautifulSoup(html, 'xml')
            try:
                regexp = "%s.*%s|%s.%s" % (keyword1, keyword2, keyword2, keyword1)
                keywordLink = soup.find('image:title', text=re.compile(regexp))
                print(keywordLink)
                return keywordLink
            except AttributeError:
                print("Product not found on site, retrying...")
                time.sleep(monitorDelay)
                self.driver.refresh()
            break

以下是im解析的xml代码:

^{pr2}$

我似乎无法到达图片:标题标签在


Tags: 代码selffalse标题htmldriver图片关键字
1条回答
网友
1楼 · 发布于 2024-09-27 22:42:35

这将查找<image:title>中的文本:

soup.findAll('image')[0].findAll('title')[0].text

或者你也可以

^{pr2}$

通过输出:

'ADIDAS YUNG-1 "CLOUD WHITE"'

您应该使用BeautifulSoupdocumentation)中的内置方法,而不是正则表达式。使用BeatifulSoup解析HTML的好处是可以利用语言的结构化形式。在

编辑

以下是完整的工作代码:

from bs4 import BeautifulSoup

html = """
<url>
<loc>
   https://packershoes.com/products/copy-of-adidas-predator-accelerator-trainer
</loc>
<lastmod>2018-11-24T08:22:42-05:00</lastmod>
<changefreq>daily</changefreq>
<image:image>
    <image:loc>
    https://cdn.shopify.com/s/files/1/0208/5268/products/adidas_Yung-1_B37616_side.jpg?v=1537395620
    </image:loc>
    <image:title>ADIDAS YUNG-1 "CLOUD WHITE"</image:title>
</image:image>
</url>
"""

soup = BeautifulSoup(html, 'xml')
soup.image.title.text

输出:

'ADIDAS YUNG-1 "CLOUD WHITE"'

相关问题 更多 >

    热门问题