【发布时间】:2019-09-10 08:04:16
【问题描述】:
我希望在 html 中的 div 标记或类元素中提取信息并在 python 中打印输出。
我想在html代码中打印“xyz”。
<div data="xyz" data-index="3" class="sg-col-20-of-24 s-result-item sg-col-0-of-12 sg-col-28-of-32 sg-col-12-of-16 sg-col-24-of-28" data-cel-widget="search_result_3"><div class="sg-col-inner">
Python 代码
soup = BeautifulSoup(r.content, 'html.parser')
data_class = soup.find_all("div", {"class": "sg-col-20-of-24 s-result-item sg-col-0-of-12 sg-col-28-of-32 sg-col-12-of-16 sg-col-24-of-28"})
for data1 in data_class:
print(data1.get('data'))
【问题讨论】:
-
你当前的代码发生了什么?有问题吗?
-
AttributeError:ResultSet 对象没有属性“get”。您可能将项目列表视为单个项目。当你打算调用 find() 时,你调用了 find_all() 吗?
标签: python html beautifulsoup python-requests