鱼C论坛

 找回密码
 立即注册
查看: 1544|回复: 4

[已解决]BeautifulSoup库find()返回列表为None

[复制链接]
发表于 2021-8-13 22:29:23 | 显示全部楼层 |阅读模式

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

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

x
  1. import requests
  2. from bs4 import BeautifulSoup

  3. headers = {
  4.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
  5.          Chrome/76.0.3809.100 Safari/537.36'
  6. }
  7. # 这里是想查的CSDN的备案信息
  8. res = requests.get('https://icp.aizhan.com/www.csdn.net/', headers = headers)
  9. # 异常处理
  10. try:
  11.     # 对返回结果进行解析
  12.     soup = BeautifulSoup(res.text, 'html.parser')
  13.     # 通过find_all()获取主办单位名称
  14.     div = soup.find('div', attrs = {'id':'icp-table'})
  15.     td_list = div.find_all('td')
  16.     name_info = td_list[0].text + ":" + td_list[1].text
  17.     print(name_info)
  18.     # 通过selector获取备案号
  19.     icp_no = soup.select('#icp-table > table > tr:nth-of-type(3) > td:nth-of-type(2) > span')[0].get_text()
  20.     print(icp_no)
  21. except ConnectionError:
  22.     print("连接失败")

复制代码


我想把网站上的指定信息爬下来,但是报错说div为None,但我检查了一下网页上面的确是有这个元素的,请问是什么原因呢?
最佳答案
2021-8-14 15:30:29
  1. import requests
  2. from bs4 import BeautifulSoup

  3. headers = {
  4.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
  5.          Chrome/76.0.3809.100 Safari/537.36'
  6.     "cookie":"将浏览器的cookie替换这里的文字"}
  7. # 这里是想查的CSDN的备案信息
  8. res = requests.get('https://icp.aizhan.com/www.csdn.net/', headers = headers)
  9. # 异常处理
  10. try:
  11.     # 对返回结果进行解析
  12.     soup = BeautifulSoup(res.text, 'html.parser')
  13.     # 通过find_all()获取主办单位名称
  14.     div = soup.find('div', attrs = {'id':'icp-table'})
  15.     td_list = div.find_all('td')
  16.     name_info = td_list[0].text + ":" + td_list[1].text
  17.     print(name_info)
  18.     # 通过selector获取备案号
  19.     icp_no = soup.select('#icp-table > table > tr:nth-of-type(3) > td:nth-of-type(2) > span')[0].get_text()
  20.     print(icp_no)
  21. except ConnectionError:
  22.     print("连接失败")


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

使用道具 举报

发表于 2021-8-14 14:02:47 | 显示全部楼层
这个url需要登陆才能出现你要的div。登陆的话可以直接用登陆点击时提交的cookie。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-14 15:30:29 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. from bs4 import BeautifulSoup

  3. headers = {
  4.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
  5.          Chrome/76.0.3809.100 Safari/537.36'
  6.     "cookie":"将浏览器的cookie替换这里的文字"}
  7. # 这里是想查的CSDN的备案信息
  8. res = requests.get('https://icp.aizhan.com/www.csdn.net/', headers = headers)
  9. # 异常处理
  10. try:
  11.     # 对返回结果进行解析
  12.     soup = BeautifulSoup(res.text, 'html.parser')
  13.     # 通过find_all()获取主办单位名称
  14.     div = soup.find('div', attrs = {'id':'icp-table'})
  15.     td_list = div.find_all('td')
  16.     name_info = td_list[0].text + ":" + td_list[1].text
  17.     print(name_info)
  18.     # 通过selector获取备案号
  19.     icp_no = soup.select('#icp-table > table > tr:nth-of-type(3) > td:nth-of-type(2) > span')[0].get_text()
  20.     print(icp_no)
  21. except ConnectionError:
  22.     print("连接失败")


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

使用道具 举报

 楼主| 发表于 2021-8-15 09:41:45 | 显示全部楼层
suchocolate 发表于 2021-8-14 14:02
这个url需要登陆才能出现你要的div。登陆的话可以直接用登陆点击时提交的cookie。

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

使用道具 举报

 楼主| 发表于 2021-8-15 09:42:19 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-20 03:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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