如何找到重复的JS元素BeautifulSoup Python



html =

<span class="title">
<a href="VIDEO HREF" title="title" class="js-pop">title text</a>"
</span>

代码 =

class Client(QWebPage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebPage.__init__(self)
self.loadFinished.connect(self.on_page_load)
self.mainFrame().load(QUrl(url))
self.app.exec_()
def on_page_load(self):
self.app.quit()
client_response = Client(url)
source = client_response.mainFrame().toHtml()
soup = bs.BeautifulSoup(source, 'lxml')
for link in soup.findAll('a', class_='js-pop'):
href = link.get('href')
print(href)
print(link.text)

我希望它返回 href 链接和标题文本。

当我运行时,它会打印每个包含文本"js-pop"的类,并且有多个名为"js-pop"的类不是我想要抓取的类。

我尝试在硒中抓取页面,当我尝试在class='js-pop'上找到href时,它打印"无">

我尝试抓取的元素都有唯一的ID,CSS选择器和xpath。

我应该如何找到这个元素?

要显示hreftitle和范围文本,您可以执行以下操作:

import bs4 as bs
html = '<span class="title"><a href="VIDEO HREF" title="title" class="js-pop">title text</a></span>'
soup = bs.BeautifulSoup(html, 'lxml')
for link in soup.findAll('a', class_='js-pop', href=True, title=True):
print(link['href'])
print(link['title'])
print(link.text)

这将显示:

VIDEO HREF
title
title text

通过添加href=Truetitle=True,它告诉查找只返回实际包含这两个属性的元素。

最新更新