python - 在 Python3 中使用 BeautifulSoup4 刮价格 Udemy 网站
问题描述
我正在尝试从 Udemy 网站提取价格数据以及学生人数。我在 Windows 上,我在 conda 环境中使用 Python 3.8 和 BeautifoulSoup。
这是我的代码:
url = 'https://www.udemy.com/course/business-analysis-conduct-a-strategy-analysis/'
html = requests.get(url).content
bs = BeautifulSoup(html, 'lxml')
searchingprice = bs.find('div', {'class':'price-text--price-part--2npPm udlite-clp-discount-price udlite-heading-xxl','data-purpose':'course-price-text'})
searchingstudents = bs.find('div', {'class':'','data-purpose':'enrollment'})
print(searchingprice)
print(searchingstudents)
而且我只得到有关学生的信息,而不是价格。我做错了什么?
None
<div class="" data-purpose="enrollment">
13,490 students
</div>
谢谢!
解决方案
价格不在源代码中,它是用 javascript 获取的。我们必须采取同样的步骤。这段代码是你自己的,bs 已经加载了
# get id of the course
course_id=bs.body.attrs['data-clp-course-id']
# build proper request, feel free to delete unneeded data requests
link=f'https://www.udemy.com/api-2.0/pricing/?course_ids={course_id}&fields[pricing_result]=price,discount_price,list_price,price_detail,price_serve_tracking_id'
# fetch the data
res=requests.get(link).json()
print(res)
>>> {'courses': {'1596446': {'_class': 'pricing_result', 'price_serve_tracking_id': 'rbNYz3yCSiS2G1J62gtSzg', 'price': {'amount': 16.99, 'currency': 'EUR', 'price_string': '€16.99', 'currency_symbol': '€'}, 'list_price': {'amount': 119.99, 'currency': 'EUR', 'price_string': '€119.99', 'currency_symbol': '€'}, 'discount_price': {'amount': 17.0, 'currency': 'EUR', 'price_string': '€17', 'currency_symbol': '€'}, 'price_detail': {'amount': 119.99, 'currency': 'EUR', 'price_string': '€119.99', 'currency_symbol': '€'}}}, 'bundles': {}}
推荐阅读
- postgresql - 当我将记录(A,B)与(B,A)相同时如何删除重复记录
- javascript - 当一个类在 Typescript 中实现自身时是什么意思
- ios - 如何获取位于 scrollIndicator 的 uiscrollView 的元素
- html - HTML/CSS:在固定 DIV 之后放置 DIV
- momentjs - 片刻。字符串到日期
- regex - 基于 Elasticsearch 字符串的查询以匹配正则表达式
- r - 在 R 中使用 'which' 和矩阵 - 'is.element()' 条件
- java - 如何复制远程目录中的所有文件?
- javascript - 图像的缩放按钮 - 不知道如何
- javascript - 如何访问 iframe 中的元素