鱼C论坛

 找回密码
 立即注册
查看: 395|回复: 1

求大神们解下,谢了,怎么改

[复制链接]
发表于 2024-4-20 01:31:22 | 显示全部楼层 |阅读模式
1鱼币
本帖最后由 忄亙 于 2024-4-20 01:58 编辑

res = requests.get(url,headers=wz)
from bs4 import BeautifulSoup
soup = BeautifulSoup(res.text,'lxml')
lis = soup.find_all('li',class_='col-4')
for li in lis:
    a = li.find_all('a')
    地址 = a.get('href')
    标题 = a.text


AttributeError: ResultSet object has no attribute 'get'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-20 01:31:47 | 显示全部楼层
这个错误是因为你把BeautifulSoup的find_all()方法返回的结果当作了一个单独的元素来处理,而实际上它返回的是一个列表。在这种情况下,你不能直接使用.get()方法。

要解决这个问题,你需要遍历这个返回的列表,然后再对每个元素使用.get()方法。下面是一个示例代码:
from bs4 import BeautifulSoup

# 假设你已经用BeautifulSoup解析了一个HTML网页,并找到了所有的<a>标签
a_tags = soup.find_all('a')

# 遍历<a>标签列表
for a_tag in a_tags:
    # 对于每个<a>标签,使用.get()方法获取想要的属性值
    href = a_tag.get('href')
    text = a_tag.get_text()
    
    # 打印属性值
    print("链接地址:", href)
    print("文本内容:", text)

这样,你就能正确地获取到每个<a>标签的属性值了。

希望这能帮到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 14:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表