【问题标题】:Unable to find specific text in meta tag using Selenium and Beatifulsoup无法使用 Selenium 和 Beautifulsoup 在元标记中找到特定文本
【发布时间】:2019-01-16 06:34:22
【问题描述】:
<div class="product-grid-item  ">

    <meta itemprop="url" content="https://undefeated.com/products/air-humara- 
     17-qs-silver-carotene-bluespark-black">

上面的代码是我要搜索的,“humara”这个词就在那里。

from Utilities import custom_logger as cl
from Base.basepage import BasePage
import logging
from bs4 import BeautifulSoup


class NewShoePage(BasePage):

    log = cl.customLogger(logging.DEBUG)

    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver

    def searchForKeywords(self, keywords):
        html = self.driver.page_source
        soup = BeautifulSoup(html, 'html.parser')
        keywordsFound = soup.find_all('meta', content=keywords)
        print(keywordsFound)

当我打电话时:

searchForKeywords('humara')

它什么也没打印出来

我想在网页的元内容标签中找到“humara”这个词,但它什么也没返回。完成后,我想重定向到该链接。

【问题讨论】:

  • 在不知道您尝试从哪个页面获取此数据的情况下,我们无法为您提供帮助。它可能是一些简单的东西,比如大写(Nike 而不是nike),因为这些东西是区分大小写的。

标签: python html selenium selenium-webdriver beautifulsoup


【解决方案1】:
def searchForKeywords(self, keywords):
    html = self.driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    possibleUrls = soup.find_all('meta', content=re.compile(keywords))
    for meta in possibleUrls:
        print(meta['content'])

以上代码返回:

https://undefeated.com/products/air-humara-17-qs-silver-carotene-bluespark-black

想通了,我不得不使用 re.compile 然后遍历 url 并将它们打印出来。

【讨论】:

    【解决方案2】:

    如果您已经在使用 Selenium,则不需要 BeautifulSoup。

    elements = driver.find_element_by_partial_link_text('nike')

    【讨论】:

    • 我试图找到的'a'标签没有显示在页面的前端,但它在html中。当我将鼠标悬停在元素上时,'a' 标记文本变得可见并且它会工作,但我不想这样做。我想在整个 html 中搜索隐藏的“a”标签中的特定关键字,然后转到该链接,希望这更清楚,谢谢
    猜你喜欢
    • 2017-11-15
    • 2021-10-24
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 2023-02-06
    相关资源
    最近更新 更多