python - 在中找不到关键字组合xml中的标记
问题描述
我想找到一个包含<loc>
两个关键字的标签。例如,我想找到一个<loc>
包含“Yankee”和“duck”的标签。代码如下:
elif len(keywords) == 2:
keyword1 = keywords[0]
keyword2 = keywords[1]
print("Searching for product...")
keywordLinkFound = False
while keywordLinkFound is False:
html = self.driver.page_source
soup = BeautifulSoup(html, 'lxml')
try:
keywordLink = soup.find('image:title', text=re.compile(keyword1 + keyword2)).text
return keywordLink
except AttributeError:
print("Product not found on site, retrying...")
time.sleep(monitorDelay)
self.driver.refresh()
break
这是我想要得到的 xml:
<url>
<loc>
https://packershoes.com/products/copy-of-382-packer-x-new-era-new-york-yankee-duck-canvas-1
</loc>
<lastmod>2018-12-06T14:39:37-05:00</lastmod>
<changefreq>daily</changefreq>
<image:image>
<image:title>
NEW ERA JAPAN 59FIFTY NEW YORK YANKEES "DUCK CANVAS"
</image:title>
</image:image>
</url>
解决方案
我会在搜索功能中执行此操作,因为它可以让您“更多”地控制搜索条件:
def desired_tags(tag):
text = tag.get_text()
return tag.name == 'image:title' and \
'Yankee' in text and 'duck' in text
results = soup.find_all(desired_tags)
推荐阅读
- python - How to rename subfolders by copying the name of the file in the subfolder?
- java - 处理程序更新片段中的 UI 滞后?
- apache-spark - 是否可以使用 PySpark 将数据插入到 couchbase 中?
- sqlite - 是否可以对输出选择进行编码?
- c# - 替换 Asp.Net Core ActionFilter 中的 RouteData 值不影响 [ApiController] 验证
- angular - 将 ngFor 与 formArray 一起使用时,垫子扩展面板不会在垫子卡内呈现
- oracle - Oracle 动态连接(或移动行)
- asp.net-core - 在 ng build --prod --configuration=development 期间生成的文件与发布目标期望复制的文件不同
- c# - 了解 NuGet 包 - 包文件夹
- php - 无法使用 find 方法或 api 平台获取我的实体?