【问题标题】:Finding a specific span element with BeautifulSoup使用 BeautifulSoup 查找特定的 span 元素
【发布时间】:2021-10-23 11:01:29
【问题描述】:

我正在尝试创建一个脚本来从 Udemy 课程中抓取价格数据。 我正在努力导航 HTML 树,因为我要查找的元素位于多个嵌套 div 中。

这是我尝试访问的 HTML 元素的结构:

我尝试了什么:

response = requests.get(url)
print(response)
doc = BeautifulSoup(response.text, "html.parser")
parent_div = doc.find(class_="sidebar-container--purchase-section--17KRp")
print(parent_div.find_all("span"))

甚至:

response = requests.get(url)
print(response)
doc = BeautifulSoup(response.text, "html.parser")
main = doc.find(class_="generic-purchase-section--main-cta-container--3xxeM")
title = main.select_one("div span span span")

这是网址:https://www.udemy.com/course/the-complete-web-development-bootcamp/

尝试搜索 HTML 中的所有跨度,但我正在搜索的特定跨度没有出现,可能是因为它嵌套在 div 中?

不胜感激!

【问题讨论】:

  • 您愿意分享实际网址吗?
  • 刚刚添加了关于错过那部分的抱歉

标签: python html web-scraping beautifulsoup


【解决方案1】:

价格正在由 JavaScript 加载。所以无法使用beautifulsoup进行抓取。

数据从API Endpoint 加载,该API Endpoint 接受课程的课程ID。

本课程的课程 ID:1565838

您可以像这样直接从该端点获取信息。

import requests

course_id = '1565838'
url= f'https://www.udemy.com/api-2.0/course-landing-components/{course_id}/me/?components=price_text'
response = requests.get(url)
x = response.json()

print(x['price_text']['data']['pricing_result']['price'])
{'amount': 455.0, 'currency': 'INR', 'price_string': '₹455', 'currency_symbol': '₹'}

【讨论】:

    【解决方案2】:

    我多次尝试了你的第一种方法,它或多或少对我有用,尽管它在不同的尝试中返回了不同数量的 span 元素(10 是通常的数字,但我看到的只有 1 对场合):

    import requests
    from bs4 import BeautifulSoup
    
    url = 'https://www.udemy.com/course/the-complete-web-development-bootcamp/'
    response = requests.get(url)
    doc = BeautifulSoup(response.text, "html.parser")
    parent_div = doc.find(class_="sidebar-container--purchase-section--17KRp")
    spans = parent_div.find_all("span")
    print(len(spans))
    for span in spans:
        print(span)
    

    打印:

    10
    <span data-checked="checked" data-name="u872-accordion--3" data-type="radio" id="u872-accordion-panel--4" style="display:none"></span>
    <span class="purchase-options--option-radio--1zjJ_ udlite-fake-toggle-input udlite-fake-toggle-radio udlite-fake-toggle-radio-small"></span>
    <span class="udlite-accordion-panel-title">Subscribe</span>
    <span>Try it free for 7 days</span>
    <span class="udlite-text-xs purchase-section-container--cta-subscript--349MH">$29.99 per month after trial</span>
    <span class="purchase-section-container--line--159eG"></span>
    <span data-checked="" data-name="u872-accordion--3" data-type="radio" id="u872-accordion-panel--6" style="display:none"></span>
    <span class="purchase-options--option-radio--1zjJ_ udlite-fake-toggle-input udlite-fake-toggle-radio udlite-fake-toggle-radio-small"></span>
    <span class="udlite-accordion-panel-title">Buy Course</span>
    <span class="money-back">30-Day Money-Back Guarantee</span>
    

    就您的第二种方法而言,您的 main div 没有那么多嵌套的 span 元素,因此它必然会失败。只尝试一个 span 元素:

    import requests
    from bs4 import BeautifulSoup
    
    url = 'https://www.udemy.com/course/the-complete-web-development-bootcamp/'
    response = requests.get(url)
    doc = BeautifulSoup(response.text, "html.parser")
    main = doc.find(class_="generic-purchase-section--main-cta-container--3xxeM")
    title = main.select_one("div span")
    print(title)
    

    打印:

    <span class="money-back">30-Day Money-Back Guarantee</span>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      • 2017-03-08
      • 2018-07-09
      • 1970-01-01
      相关资源
      最近更新 更多