我想把图片:标题标签包含来自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}$我似乎无法到达图片:标题标签在
这将查找
<image:title>
中的文本:或者你也可以
^{pr2}$通过输出:
您应该使用
BeautifulSoup
(documentation)中的内置方法,而不是正则表达式。使用BeatifulSoup
解析HTML
的好处是可以利用语言的结构化形式。在编辑
以下是完整的工作代码:
输出:
相关问题 更多 >
编程相关推荐