python - BeautifulSoup 在 HTML 中找不到元素类

标签 python beautifulsoup

我正在尝试抓取此页面有 10 个 class='name main-name',如下所示:sample source

但是当我编码时:

import requests
from bs4 import BeautifulSoup

result = requests.get("https://genvita.vn/thu-thach/7-ngay-detox-da-dep-dang-thon-nguoi-khoe-qua-soc-len-den-8-trieu-dong")

c = result.text
soup = BeautifulSoup(c, "html.parser")

comment_items = soup.find_all('div', class_="name main-name")
print(len(comment_items)

但是返回:0而不是返回:10。我尝试过在stackoverflow中搜索并使用许多解决方案,但无法修复

最佳答案

因为 div name main-name 没有出现在您的 DOM 中。在这种情况下,使用 SeleniumBeautifulSoap

更强大
from  selenium import webdriver

driver_path = r'Your Chrome driver path'
browser = webdriver.Chrome(executable_path=driver_path)
browser.get("https://genvita.vn/thu-thach/7-ngay-detox-da-dep-dang-thon-nguoi-khoe-qua-soc-len-den-8-trieu-dong")

get_element  = browser.find_elements_by_css_selector("div[class='name main-name']")
print len(get_element)

browser.close()

输出:

10

您还可以获得如下名称:

 for users in get_element:
    print(users.text)

输出:

Phạm Thị Kim Chi
My Linh Nguyen
Mr Vinh Bảo Hiểm Sức Khoẻ Sắc Đẹp
Ngô Thị Tuyết
Huỳnh Thị Bích Trâm
Linh Trúc Diêm
Nguyen Tu
Nguyen Thom
Hồ Thu Trang
Trầnthịtrắng

关于python - BeautifulSoup 在 HTML 中找不到元素类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54886274/

相关文章:

python - 如何创建在单击按钮时显示动态数据的 View

python - 循环访问 BeautifulSoup 中的元素,但仅输出该元素的子元素

Python Web Scraping - 试图从表格标签中获取数字

python - 是什么导致我的 Python 代码在编译时出现 AttributeError?

python - 设置属于某个类的类的最有效方法是什么

python - 我可以填写输入字段并单击表单中没有的提交按钮吗?

python - BeautifulSoup 中未显示表元素

python - 使用 BS4 获取属性名称而不是值

python - django.core.urlresolvers.NoReverseMatch

python - 在matplotlib中更改虚线中的破折号间距