【问题标题】:How to scrape data from a dynamic website with Selenium如何使用 Selenium 从动态网站中抓取数据
【发布时间】:2020-08-08 11:26:25
【问题描述】:

我是 selenium 新手,想从 Udemy 课程链接刮价格提供结束时间。我该怎么做?

价格和课程结束时间会动态加载到网站。我知道如何从网站中提取简单内容,但不知道动态内容。

我已尝试使用 Parsel 库 + Seleminium 库,但返回空字符串。因为当我在我的手机中查看网站的源代码时,源代码中没有显示价格。但是当我点击 chrome 或 firefox 的检查元素选项时。价格在跨度标签内提供。意味着当页面在浏览器上呈现时,价格是动态加载的。我如何在 Selenium 中做到这一点?

这是一个示例 Udemy 课程链接:

https://www.udemy.com/course/data-science-deep-learning-in-python/

【问题讨论】:

  • 嗨!请向我们展示您尝试过的内容,提供数据和示例代码,以及您尝试过的研究。请参阅此帖子:stackoverflow.com/help/how-to-ask,了解我们如何为您提供最好的帮助。
  • 我已经提供了所有信息。现在请回答这个问题..
  • 你能在这里分享你的代码段吗?
  • 为什么不使用udemy-dl - github.com/r0oth3x49/udemy-dl
  • 因为我想自己做...我不能在这里分享我的代码段...抱歉...

标签: python python-3.x selenium web-scraping selenium-chromedriver


【解决方案1】:

在您的环境中已安装所有依赖项后,此代码应该可以工作:

    from selenium import webdriver
    from bs4 import BeautifulSoup
    from webdriver_manager.chrome import ChromeDriverManager

    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get("https://www.udemy.com/course/appium-selenium-for-mobile-automation-testing/")

    content = driver.page_source

    soup = BeautifulSoup(content, 'html.parser')

    price = soup.find('div', {'class':'price-text--price-part--Tu6MH udlite-clp-discount-price udlite-heading-xl'})
    if price is not None:
        price.text.strip()
        price = price.replace('Current price','')
        print('Price: ' + price)
        
        offerEndTime = soup.find('span', {'data-purpose':'safely-set-inner-html:discount-expiration:expiration-text'}).text.strip()
        print('Offer end time: ' +  offerEndTime)
    else:
        print('This is a free course')

【讨论】:

  • 兄弟,这个链接可以正常工作......但是当我用另一个网址测试它时,它会显示错误......任何解决方案......
  • 你试过哪个网址?我用其他网址测试过,它仍然有效
  • URL 是 udemy.com/course/appium-selenium-for-mobile-automation-testing 错误是 2020-08-08T15:43:39.189996+00:00 app[worker.1]: price = soup2.find('div', {'class':'price-text--price-part--Tu6MH udlite-clp-discount-price udlite-heading-xl'}).text.strip() 2020-08-08T15:43:39.189996+00:00 app[worker.1]: AttributeError: 'NoneType' object has no attribute 'text' 我在 Heroku 上运行
  • 这是免费课程,这就是为什么我遇到错误对不起兄弟......现在我的问题已经解决了......
  • 如果课程像这样永久免费,我如何打印获得 免费
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 1970-01-01
  • 2019-06-29
  • 2019-01-18
  • 1970-01-01
  • 2019-10-31
  • 2021-03-26
相关资源
最近更新 更多