gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:30 24 4
gpt4 key购买 nike

我正在尝试抓取此页面有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com