Python Beautifulsoup 用西班牙语拼写 class

Python Beautifulsoup Scrapping a class in span

我正在尝试获取 udemy 课程的评分。例如:https://www.udemy.com/course/the-modern-cpp-20-masterclass/ 遗憾的是,我的查找行中出现语法错误。 等你帮忙:)

import time      
import os         
import bs4
from bs4 import BeautifulSoup  
import pandas as pd
import scipy as sc
import numpy as np
import requests
url = "https://www.udemy.com/course/the-modern-cpp-20-masterclass/"
page = requests.get(url)
soup = BeautifulSoup(page.content,'html.parser')


#<span class="udlite-heading-sm star-rating--rating-number--2o8YM" aria-hidden="true" data-purpose="rating-number">4.7</span>course_rating_avarage = soup.find('div', {"class" : udlite-sr-only})
course_rating_avarage = soup.find('span', {"class" : udlite-heading-sm star-rating--rating-number--2o8YM})
course_rating_avarage_text = course_rating_avarage.string
print (course_rating_avarage_text)

soup.find() 中的参数必须作为字符串引用。

import bs4
from bs4 import BeautifulSoup  
import requests

url = "https://www.udemy.com/course/the-modern-cpp-20-masterclass/"
page = requests.get(url)
soup = BeautifulSoup(page.content,'html.parser')

course_rating_avarage = soup.find('span', {"class" : "udlite-heading-sm star-rating--rating-number--2o8YM"})
#                                                    ^ quote the value above
course_rating_avarage_text = course_rating_avarage.string
print (course_rating_avarage_text)

输出:

4.7

Class 类型会随时间变化,因此最好在 data-purpose 属性中查找 'rating-number' 值。

course_rating_avarage = soup.find('span', {"data-purpose" : "rating-number"})

所以,我通过编辑 course_rating_avarage 字段修复了它:

course_rating_avarage = soup.find('span', {"class" : 'udlite-heading-sm star-rating--rating-number--2o8YM'})