鱼C论坛

 找回密码
 立即注册
查看: 1988|回复: 5

[已解决]BS4提取src全部链接总是Tag报错,新手求大神解决

[复制链接]
发表于 2021-3-14 21:09:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. from  bs4 import BeautifulSoup

  3. url = "https://www.itotii.net/584.html"
  4. headres = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0'}

  5. res=requests.get(url,headers=headres)
  6. res.encoding = 'utf-8'
  7. print(res.status_code)

  8. soup = BeautifulSoup(res.text,'html.parser')
  9. item = soup.find_all(class_="article-content")
  10. print(item)

  11. for src in item:
  12.     herf = item.find_all(data-tag="bdshare")
  13.     print(herf['src'])
复制代码
最佳答案
2021-3-15 18:33:20
网页源代码img标签不存在属性data-tag。
  1. import requests
  2. from  bs4 import BeautifulSoup

  3. url = "https://www.itotii.net/584.html"
  4. headres = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0'}
  5. res = requests.get(url, headers=headres)
  6. soup = BeautifulSoup(res.text,'html.parser')
  7. item = soup.find_all(class_="article-content")[0]
  8. imgs = item.find_all("img")
  9. for img in imgs:
  10.     herf = img["src"]
  11.     print(herf)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 11:56:19 | 显示全部楼层
报错不是正常的么
  1. for src in item:        
  2.     herf = item.find_all(data-tag="bdshare")  # find_all()方法返回的是列表
  3.     print(herf['src'])          # 所以这句就报错了咯?
复制代码

改为下面代码试试看:
  1. herf = item[0].find_all(data-tag="bdshare")
  2. for each in herf:
  3.     print(each['src'])
复制代码


没装BeautifulSoup模块没法测试,你试试吧,应该没问题的,现在改用xpath了,大多数时候还是比较方便的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 18:33:20 | 显示全部楼层    本楼为最佳答案   
网页源代码img标签不存在属性data-tag。
  1. import requests
  2. from  bs4 import BeautifulSoup

  3. url = "https://www.itotii.net/584.html"
  4. headres = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0'}
  5. res = requests.get(url, headers=headres)
  6. soup = BeautifulSoup(res.text,'html.parser')
  7. item = soup.find_all(class_="article-content")[0]
  8. imgs = item.find_all("img")
  9. for img in imgs:
  10.     herf = img["src"]
  11.     print(herf)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-15 20:02:07 | 显示全部楼层
YunGuo 发表于 2021-3-15 18:33
网页源代码img标签不存在属性data-tag。

你好
  1. item = soup.find_all(class_="article-content")[0]
复制代码
为什么这个地方加0下面就可以运行 不加就运行不了呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 23:54:33 | 显示全部楼层
南朴 发表于 2021-3-15 20:02
你好    为什么这个地方加0下面就可以运行 不加就运行不了呢?

find_all()返回的是一个列表啊,加0是为了索引,把列表中的数据取出来。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-16 18:59:05 | 显示全部楼层
YunGuo 发表于 2021-3-15 23:54
find_all()返回的是一个列表啊,加0是为了索引,把列表中的数据取出来。

嗯嗯 谢谢谢谢  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 12:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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