鱼C论坛

 找回密码
 立即注册
查看: 1337|回复: 3

[已解决]小甲鱼课程中的代码问题

[复制链接]
发表于 2020-2-6 14:39:15 | 显示全部楼层 |阅读模式

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

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

x
运行爬虫时,就会报错:
  File "D:\Python(zzt)\甲鱼文件任务\爬虫\GetTop250.py", line 45, in find_depth
    soup = bs4.BeautifulSoup(res.text,'html.parser')
AttributeError: 'NoneType' object has no attribute 'text'

还请各位大神帮忙指点!!!
(下面是源代码)

import requests
import bs4
import re
def open_url(url):
    headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    res = requests.get(url,headers=headers)

def find_movies(res):
    soup=bs4.BeautifulSoup(res.text,'html.parser')

    #获取电影名
    movies=[]
    target = soup.find_all('div',class_='hd')
    for each in target:
        movies.append(each.a.span.text)
        
    #获取电影评分
    ranks=[]
    target = soup.fina_all('span',class_='rating_num')
    for each in target:
        ranks.append('%s' % each.text)
        
    #获取电影资料
    message=[]
    target = soup.find_all('div',class_='bd')
    for each in target:
        try:      
            message.append(each.p.text.split('\n')[1].strip()+each.p.text.split('\n')[2].strip())
        except:
            continue
        
    result=[]
    length = len(movies)
    for i in range(length):
       result.append(movies[i]+ranks[i]+message[i]+'\n')
    return result



#找出一共有多少个页面
def find_depth(res):
    soup = bs4.BeautifulSoup(res.text,'html.parser')
    depth = soup.find('span',class_='next').previous_sibling.text
    return int(depth)


   


def main():
    host='https://movie.douban.com/top250'
    res=open_url(host)
    depth = find_depth(res)
    result = []
    for i in range(depth):
        url=host+ '?start='+str(25*i)+'&filter='
        res = open_url(url)
        result.extend(find_movies(res))
        
        
    with open ('豆瓣250爬虫下载.txt','w',encode='utf-8')as f:
        for each in result:
            print('打印成功')
            f.write(each)
   
if __name__=='__main__':
    main()
最佳答案
2020-2-6 14:43:43
这样试试:

  1. import requests
  2. import bs4
  3. import re
  4. def open_url(url):
  5.     headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
  6.     res = requests.get(url,headers=headers)
  7.     return res

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

  10.     #获取电影名
  11.     movies=[]
  12.     target = soup.find_all('div',class_='hd')
  13.     for each in target:
  14.         movies.append(each.a.span.text)
  15.         
  16.     #获取电影评分
  17.     ranks=[]
  18.     target = soup.fina_all('span',class_='rating_num')
  19.     for each in target:
  20.         ranks.append('%s' % each.text)
  21.         
  22.     #获取电影资料
  23.     message=[]
  24.     target = soup.find_all('div',class_='bd')
  25.     for each in target:
  26.         try:      
  27.             message.append(each.p.text.split('\n')[1].strip()+each.p.text.split('\n')[2].strip())
  28.         except:
  29.             continue
  30.         
  31.     result=[]
  32.     length = len(movies)
  33.     for i in range(length):
  34.        result.append(movies[i]+ranks[i]+message[i]+'\n')
  35.     return result



  36. #找出一共有多少个页面
  37. def find_depth(res):
  38.     soup = bs4.BeautifulSoup(res.text,'html.parser')
  39.     depth = soup.find('span',class_='next').previous_sibling.text
  40.     return int(depth)


  41.    


  42. def main():
  43.     host='https://movie.douban.com/top250'
  44.     res=open_url(host)
  45.     depth = find_depth(res)
  46.     result = []
  47.     for i in range(depth):
  48.         url=host+ '?start='+str(25*i)+'&filter='
  49.         res = open_url(url)
  50.         result.extend(find_movies(res))
  51.         
  52.         
  53.     with open ('豆瓣250爬虫下载.txt','w',encode='utf-8')as f:
  54.         for each in result:
  55.             print('打印成功')
  56.             f.write(each)
  57.    
  58. if __name__=='__main__':
  59.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-6 14:43:43 | 显示全部楼层    本楼为最佳答案   
这样试试:

  1. import requests
  2. import bs4
  3. import re
  4. def open_url(url):
  5.     headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
  6.     res = requests.get(url,headers=headers)
  7.     return res

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

  10.     #获取电影名
  11.     movies=[]
  12.     target = soup.find_all('div',class_='hd')
  13.     for each in target:
  14.         movies.append(each.a.span.text)
  15.         
  16.     #获取电影评分
  17.     ranks=[]
  18.     target = soup.fina_all('span',class_='rating_num')
  19.     for each in target:
  20.         ranks.append('%s' % each.text)
  21.         
  22.     #获取电影资料
  23.     message=[]
  24.     target = soup.find_all('div',class_='bd')
  25.     for each in target:
  26.         try:      
  27.             message.append(each.p.text.split('\n')[1].strip()+each.p.text.split('\n')[2].strip())
  28.         except:
  29.             continue
  30.         
  31.     result=[]
  32.     length = len(movies)
  33.     for i in range(length):
  34.        result.append(movies[i]+ranks[i]+message[i]+'\n')
  35.     return result



  36. #找出一共有多少个页面
  37. def find_depth(res):
  38.     soup = bs4.BeautifulSoup(res.text,'html.parser')
  39.     depth = soup.find('span',class_='next').previous_sibling.text
  40.     return int(depth)


  41.    


  42. def main():
  43.     host='https://movie.douban.com/top250'
  44.     res=open_url(host)
  45.     depth = find_depth(res)
  46.     result = []
  47.     for i in range(depth):
  48.         url=host+ '?start='+str(25*i)+'&filter='
  49.         res = open_url(url)
  50.         result.extend(find_movies(res))
  51.         
  52.         
  53.     with open ('豆瓣250爬虫下载.txt','w',encode='utf-8')as f:
  54.         for each in result:
  55.             print('打印成功')
  56.             f.write(each)
  57.    
  58. if __name__=='__main__':
  59.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-6 14:48:56 | 显示全部楼层

还是会报错:
  File "D:\Program Files (x86)\python\lib\site-packages\bs4\element.py", line 871, in __getattr__
    self.__class__.__name__, attr))
AttributeError: 'NavigableString' object has no attribute 'text'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-6 14:55:21 | 显示全部楼层
本帖最后由 ColaPlusIce 于 2020-2-6 14:57 编辑

可以了,感谢!
encoding 错打成了encode
import requests
import bs4
import re
def open_url(url):
    headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    res = requests.get(url,headers=headers)
    return res

def find_movies(res):
    soup=bs4.BeautifulSoup(res.text,'html.parser')

    #获取电影名
    movies=[]
    target = soup.find_all('div',class_='hd')
    for each in target:
        movies.append(each.a.span.text)
        
    #获取电影评分
    ranks=[]
    target = soup.find_all('span',class_='rating_num')
    for each in target:
        ranks.append('%s' % each.text)
        
    #获取电影资料
    message=[]
    target = soup.find_all('div',class_='bd')
    for each in target:
        try:      
            message.append(each.p.text.split('\n')[1].strip()+each.p.text.split('\n')[2].strip())
        except:
            continue
        
    result=[]
    length = len(movies)
    for i in range(length):
       result.append(movies[i]+ranks[i]+message[i]+'\n')
    return result



#找出一共有多少个页面
def find_depth(res):
    soup = bs4.BeautifulSoup(res.text,'html.parser')
    depth = soup.find('span',class_='next').previous_sibling.previous_sibling.text
    return int(depth)


   


def main():
    host='https://movie.douban.com/top250'
    res=open_url(host)
    depth = find_depth(res)
    result = []
    for i in range(depth):
        url=host+ '?start='+str(25*i)+'&filter='
        res = open_url(url)
        result.extend(find_movies(res))
        
        
    with open ('豆瓣250爬虫下载.txt','w',encoding='utf-8')as f:
        for each in result:
            print('打印成功')
            f.write(each)
   
if __name__=='__main__':
    main()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-4 01:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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