python - 页面源不显示 selenium/Python 的广告

标签 python selenium selenium-webdriver web-scraping

这应该是一个非常直接的元素查找,只是没有发生,我添加了一个很长的隐式等待以允许页面完全加载

from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("https://www.smh.com.au")
driver.find_elements_by_class_name("img_ad")

以及根据元素位置等待加载

timeout = 10    
try:
    element_present = EC.presence_of_element_located((By.CLASS_NAME, '"img_ad'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print("Timed out waiting for page to load")

但是,尽管我在 Firefox 的检查模式下清楚地看到了该元素,但它并未出现

<img src="https://tpc.googlesyndication.com/simgad/9181016285467049325" alt="" class="img_ad" width="970" height="250" border="0">

这是页面上的广告,所以我认为上面可能有一些奇怪的代码,这些代码没有显示在驱动程序中,关于如何收集它有什么建议吗?

最佳答案

广告位于 iFrame 中,因此您需要先切换此框架。

但我发现在几次页面加载后,广告就不再出现在网页上。我确实发现,几乎每次使用 driver = webdriver.Opera() 都会加载广告,但在 Firefox 的 Chrome 中却不会,甚至使用隐私浏览并清除所有浏览数据也是如此。

如果它们出现,那么这段代码就起作用了。

为了通过部分类名查找元素,我首先使用了 find_element_by_css_selector("amp-img[class^='img_ad']")。有时,具有 img_ad 类的元素不存在,因此您可以使用 driver.find_element_by_id("aw0") 来更频繁地查找数据。有时网页 HTML 甚至没有这个 id,所以我的代码会打印 HTML。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException


driver = webdriver.Firefox()
driver.get("https://www.smh.com.au")
driver.implicitly_wait(10)

iFrame = driver.find_elements_by_tag_name("iframe")[1]
driver.switch_to.frame(iFrame)

try:
    # element = driver.find_element_by_css_selector("amp-img[class^='img_ad']")
    # print(element.get_attribute('outerHTML'))
    element = driver.find_element_by_id("aw0")
    print(element.get_attribute('innerHTML'))
except NoSuchElementException:
    print("Advert not found")
    print(driver.page_source)

driver.quit()

输出:

<amp-img alt="" class="img_ad i-amphtml-layout-fixed i-amphtml-layout-size-defined i-amphtml-element i-amphtml-layout" height="250" i-amphtml-layout="fixed" i-amphtml-ssr="" src="https://tpc.googlesyndication.com/simgad/16664324514375864185" style="width:970px;height:250px;" width="970"><img alt="" class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://tpc.googlesyndication.com/simgad/16664324514375864185"></amp-img>

或者:

<img src="https://tpc.googlesyndication.com/simgad/10498242030813793376" border="0" width="970" height="250" alt="" class="img_ad">

或者:

<html><head></head><body></body></html>

关于python - 页面源不显示 selenium/Python 的广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60015495/

相关文章:

python - 如何使用 Beatbox 或 simple-salesforce 在 Salesforce 中创建自定义表格?

java - Selenium 页面对象。如何从外部源读取@FindBy 定位器?

Selenium - 如何在 WebDriver 中通过 href 值单击链接

ruby-on-rails - 如何跳过 cucumber 场景中的登录以加快测试速度?

java - 未从 div 获取值

python - 如何安装 'adium-theme-ubuntu' (virtualenv)

Python 和多处理示例

Python while 循环语法错误

python - 网络驱动程序,Python : How to catch dynamically updating url with the appropriate value and save it to the variable

javascript - 如何使用 Selenium 和 Python 抓取传单 map 多边形