访问BeautifulSoup4中多个div中的div

2024-03-29 08:51:48 发布

您现在位置:Python中文网/ 问答频道 /正文

大约4天前,我刚刚为我的项目学习了BeautifulSoup和Python,现在在从这里访问多个div时遇到了困难:

enter image description here

我的目的是从
获取“DIY项目”的价值,因此我尝试通过以下方式访问:

AccName = soup.find('div', {'class' : 'comProfile_hld row'})
print(AccName.get_text())

通过这段代码,我得到:AttributeError: 'NoneType' object has no attribute 'get_Text'

然后我将打印部分更改为:print(AccName),但结果是“无”

那么,如何直接访问具有多层div的值呢?beautifulsoup4真的可以通过div名称访问div吗

非常感谢


Tags: 项目目的divget方式findclass价值
1条回答
网友
1楼 · 发布于 2024-03-29 08:51:48

该特定信息来自一个ajax请求,您可以在“网络”选项卡中找到该请求

https://www.bursamarketplace.com/index.php?tpl=company_ajax&type=profile_address&code=THRI.KL

因此,在返回的html上应该可以使用如下内容

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('https://www.bursamarketplace.com/index.php?tpl=company_ajax&type=profile_address&code=THRI.KL')
soup = bs(r.content, 'html.parser')
print(soup.select_one('.cttDetail_label').next_sibling.next_sibling)

相关问题 更多 >