鱼C论坛

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

[已解决]爬虫标签

[复制链接]
发表于 2020-8-9 17:13:16 | 显示全部楼层 |阅读模式
10鱼币
  1. <div class="p-price">
  2.        <strong class="J_100012820022" data-done="1">
  3.         <em>¥</em><i>3298.00</i>
  4.        </strong>
  5.       </div>
复制代码
上面这个标签应该怎么找到3298.00,并且可以跳过<strong class="J_100012820022" data-done="1">?

  1. price=[]
  2.     price=soup.find_all('div',class_="p-price")
  3.     for pri in price:
  4.         price.append(pri.i)
复制代码
我是找到<div class="p-price">这个标签,然后pri.i直接找i这个节点,但是会报错AttributeError: 'NoneType' object has no attribute 'i'
price.append(pri.i.text)这样也会报错AttributeError: 'NoneType' object has no attribute 'text'
但是直接print(pri.i)却是有结果?
想知道原因?以及解决方法?


最佳答案
2020-8-9 17:13:17


用 try-except 过滤下 None 的即可

  1. import requests
  2. import bs4
  3. import time


  4. def open_url(url):
  5.     headers = {
  6.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3706.400 SLBrowser/10.0.4040.400'}
  7.     res = requests.get(url, headers=headers)
  8.     return res


  9. def find_phone(res):
  10.     soup = bs4.BeautifulSoup(res.text, 'html.parser')

  11.     # 手机名
  12.     names = []
  13.     name = soup.find_all('div', class_="p-name p-name-type-3")
  14.     for each in name:
  15.         names.append(each.a.em.text)

  16.     # 价格
  17.     price = []
  18.     price = soup.find_all('div', class_="p-price")
  19.     for pri in price:
  20.         try:
  21.             price.append(pri.i.text)
  22.         except:
  23.             pass
  24.     # 将找到的每一页的内容放进result中
  25.     result = []
  26.     length = len(names)
  27.     for i in range(length):
  28.         result.append(names[i] + ':' + '\n' + str(price[i]) + '\n')
  29.     return result


  30. def main():
  31.     result = []
  32.     url = 'https://list.jd.com/list.html?cat=9987%2C653%2C655&page=1&s=1&click=0'
  33.     res = open_url(url)
  34.     result.extend(find_phone(res))

  35.     with open("E:\京东手机.txt", "w", encoding="utf-8") as f:
  36.         for each in result:
  37.             f.write(each)

  38.     time.sleep(1)


  39. if __name__ == "__main__":
  40.     main()
复制代码

最佳答案

查看完整内容

用 try-except 过滤下 None 的即可
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-9 17:13:17 | 显示全部楼层    本楼为最佳答案   


用 try-except 过滤下 None 的即可

  1. import requests
  2. import bs4
  3. import time


  4. def open_url(url):
  5.     headers = {
  6.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3706.400 SLBrowser/10.0.4040.400'}
  7.     res = requests.get(url, headers=headers)
  8.     return res


  9. def find_phone(res):
  10.     soup = bs4.BeautifulSoup(res.text, 'html.parser')

  11.     # 手机名
  12.     names = []
  13.     name = soup.find_all('div', class_="p-name p-name-type-3")
  14.     for each in name:
  15.         names.append(each.a.em.text)

  16.     # 价格
  17.     price = []
  18.     price = soup.find_all('div', class_="p-price")
  19.     for pri in price:
  20.         try:
  21.             price.append(pri.i.text)
  22.         except:
  23.             pass
  24.     # 将找到的每一页的内容放进result中
  25.     result = []
  26.     length = len(names)
  27.     for i in range(length):
  28.         result.append(names[i] + ':' + '\n' + str(price[i]) + '\n')
  29.     return result


  30. def main():
  31.     result = []
  32.     url = 'https://list.jd.com/list.html?cat=9987%2C653%2C655&page=1&s=1&click=0'
  33.     res = open_url(url)
  34.     result.extend(find_phone(res))

  35.     with open("E:\京东手机.txt", "w", encoding="utf-8") as f:
  36.         for each in result:
  37.             f.write(each)

  38.     time.sleep(1)


  39. if __name__ == "__main__":
  40.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-9 17:22:22 | 显示全部楼层
本帖最后由 Twilight6 于 2020-8-9 17:23 编辑



加上 .text 是没错的,你报错的原因是因为有的没有爬到数据,所以返回 None ,而你对 None 直接 .text 肯定会导致报错的

发个完整代码看看吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-8-9 17:25:44 | 显示全部楼层
Twilight6 发表于 2020-8-9 17:22
加上 .text 是没错的,你报错的原因是因为有的没有爬到数据,所以返回 None ,而你对 None 直接 .text  ...
  1. import requests
  2. import bs4
  3. import time

  4. def open_url(url):
  5.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3706.400 SLBrowser/10.0.4040.400'}
  6.     res = requests.get(url, headers=headers)
  7.     return  res

  8. def find_phone(res):
  9.     soup=bs4.BeautifulSoup(res.text, 'html.parser')

  10.     #手机名
  11.     names=[]
  12.     name=soup.find_all('div',class_="p-name p-name-type-3")
  13.     for each in name:
  14.         names.append(each.a.em.text)
  15.         

  16.     #价格
  17.     price=[]
  18.     price=soup.find_all('div',class_="p-price")
  19.     for pri in price:
  20.         price.append(pri.i.text)
  21.         

  22.     #将找到的每一页的内容放进result中
  23.     result = []
  24.     length = len(names)
  25.     for i in range(length):
  26.         result.append(names[i]+':'+'\n'+price[i] + '\n')
  27.     return result   


  28. def main():
  29.     result=[]
  30.     url='https://list.jd.com/list.html?cat=9987%2C653%2C655&page=1&s=1&click=0'
  31.     res = open_url(url)
  32.     result.extend(find_phone(res))
  33.    
  34.     with open("E:\京东手机.txt", "w", encoding="utf-8") as f:
  35.         for each in result:
  36.             f.write(each)

  37.     time.sleep(1)
  38.         
  39. if __name__ == "__main__":
  40.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-8-9 17:42:42 | 显示全部楼层
Twilight6 发表于 2020-8-9 17:36
用 try-except 过滤下 None 的即可

是因为找到的有几个是None吗?所以过滤掉报错的信息
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-9 17:44:50 | 显示全部楼层
月魔同学 发表于 2020-8-9 17:42
是因为找到的有几个是None吗?所以过滤掉报错的信息

嗯 None 不能 .text
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-25 05:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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