鱼C论坛

 找回密码
 立即注册
查看: 1560|回复: 6

[已解决]求大神们解下,谢了,怎么改

[复制链接]
发表于 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()?
最佳答案
2024-4-20 01:31:23
你这里报错是因为:find_all('a') 返回的是列表(ResultSet),但你后面把它当成单个标签来用 (a.get(...) / a.text)。

只取每个 <li> 里的第一个链接:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. from urllib.parse import urljoin

  4. res = requests.get(url, headers=wz, timeout=10)
  5. res.raise_for_status()

  6. soup = BeautifulSoup(res.text, 'lxml')
  7. lis = soup.find_all('li', class_='col-4')

  8. data = []
  9. for li in lis:
  10.     a = li.find('a', href=True)   # 用 find() 返回单个元素
  11.     if not a:
  12.         continue
  13.     地址 = urljoin(url, a['href']) # 兼容相对链接
  14.     标题 = a.get_text(strip=True)
  15.     data.append({'标题': 标题, '地址': 地址})

  16. print(data)
复制代码

最佳答案

查看完整内容

你这里报错是因为:find_all('a') 返回的是列表(ResultSet),但你后面把它当成单个标签来用 (a.get(...) / a.text)。 只取每个 里的第一个链接:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-4-20 01:31:23 | 显示全部楼层    本楼为最佳答案   
你这里报错是因为:find_all('a') 返回的是列表(ResultSet),但你后面把它当成单个标签来用 (a.get(...) / a.text)。

只取每个 <li> 里的第一个链接:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. from urllib.parse import urljoin

  4. res = requests.get(url, headers=wz, timeout=10)
  5. res.raise_for_status()

  6. soup = BeautifulSoup(res.text, 'lxml')
  7. lis = soup.find_all('li', class_='col-4')

  8. data = []
  9. for li in lis:
  10.     a = li.find('a', href=True)   # 用 find() 返回单个元素
  11.     if not a:
  12.         continue
  13.     地址 = urljoin(url, a['href']) # 兼容相对链接
  14.     标题 = a.get_text(strip=True)
  15.     data.append({'标题': 标题, '地址': 地址})

  16. print(data)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

  1. from bs4 import BeautifulSoup

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

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


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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-8-19 09:46:20 | 显示全部楼层
  1. res = requests.get(url,headers=wz)
  2. from bs4 import BeautifulSoup
  3. soup = BeautifulSoup(res.text,'lxml')
  4. lis = soup.find_all('li',class_='col-4')
  5. for li in lis:
  6.     a = li.find_all('a')
  7.     <font color="#ff0000">地址</font> = a.get('href')
  8.     <font color="#ff0000">标题</font> = a.text
复制代码
变量不能用中文命名
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-8-19 14:35:29 | 显示全部楼层

py3支持中文命名了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-8-19 15:53:24 | 显示全部楼层

挖丢,我现在才知道,那我就找不出问题了,我没学过bs4和requests库
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-8-21 21:12:36 | 显示全部楼层
冲浪的口香糖 发表于 2025-8-19 15:53
挖丢,我现在才知道,那我就找不出问题了,我没学过bs4和requests库

真的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-4 01:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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