python这个报错该怎么解决啊?

目的时爬取网页的表格信息,但总是报错AttributeError: 'NoneType' object has no attribute 'find_all',网上搜了好多都没用,还是报错,快疯了,啊啊啊啊啊

这是我的源代码,求大佬帮忙解决一下,万分感谢!!!!

#大作业程序:从网页中获取电影排名及类型等信息
import requests
from bs4 import BeautifulSoup
import openpyxl
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
         'Accept - Encoding':'gzip, deflate, br'}
def get_html(url):#访问网站获取网站源代码内容
    try:
       r=requests.get(url,headers=header)
       r.raise_for_status()
       r.encoding=r.apparent_encoding
       return r.text
    except:
        r="fail"
        return r
def get_contents(ulist,rurl):#解析源代码,找到表格内容并提取信息
    soup=BeautifulSoup(rurl,'html.parser')
    trs=soup.find('tr',class_='current').find_all('tr')
    for tr in trs:
        ui=[]
        for td in tr:
            ts=td.string
            ts=ts.strip()
            if ts=='':
                continue
            ui.append(ts)
            ulist.append(ui)
    print(ulist)
def baocun(ulist):#将信息储存到表中
    wb=openpyxl.Workbook()
    ws=wb.active
    r=1
    for line in ulist:
        for col in range (1,len(line)+1):
            ws.cell(row=r,column=col).value=line[col-1]
        r=r+1
    wb.save('python大作业.xlsx')
#调用函数,进行爬取
urli=[]
url="https://www.endata.com.cn/BoxOffice/BO/Year/index.html"
rs=get_html(url)
get_contents(urli,rs)
baocun(urli)
print(urli)

 

没有找到这样的元素。在页面里。

 数据都在这个里面。。。

https://blog.csdn.net/qq_26079939/article/details/114934545

参考这个爬虫

import requests
import os
import execjs

def get_data(YEAR):
    
    for i in range(14):
        data = {
        'year': YEAR,
        'MethodName': 'BoxOffice_GetYearInfoData'
        }

        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63'}

        url = 'https://www.endata.com.cn/API/GetData.ashx'

        response = requests.post(url,headers=headers,data=data)

        result = response.text

      #  print(result)

        with open("a.js","r", encoding="utf-8") as f:

            js = f.read()
            # 编译js代码

            resp = execjs.compile(js)
            print(resp.call('b',result))

        # 调用方法

        #   response = resp.call('',1)
    
            print(response)

            YEAR = YEAR + 1

def main():

        YEAR = 2008

        get_data(YEAR)

if __name__ == '__main__':
       
        os.environ["NODE_PATH"] = os.getcwd()+"/node_modules"
        main()

使用execjs跑js跑不出来 移步node进行操作了。

https://blog.csdn.net/codingpy/article/details/107679292 

这里是我跟着文档一起写的js 要配的东西比较多。

a.js的文件过大我贴在云盘里了。

链接:https://pan.baidu.com/share/init?surl=NUub2SL9oc40ZwBGM4d4Yw
提取码:i6o8 
复制这段内容后打开百度网盘手机App,操作更方便哦

 

我个人环境是Jupyter notebook + node.js

如果楼主用的 pycharm可能需要对node.js进行配置一下才行。

//5. 方式三:Node.js
//实际上是使用 Python 的os.popen执行 node 命令,执行 JS 脚本

//首先,确保本地已经安装了 Node.js 环境

//修改 JS 脚本,新增一个导出函数 init ,方便内部函数被调用

//计算两个数的和
function add(num1, num2) {
    return num1 + num2;
}
 
//新增一个导出函数(node方式)
module.exports.init = function (arg1, arg2) {
    //调用函数,并返回
    console.log(add(arg1, arg2));
};
//然后,将调用 JS 方法的命令组成一个字符串
# 然后,将调用 JS 方法的命令组成一个字符串

# 组成调用js的命令
# node命令:node -e
cmd = 'node -e "require(\\"%s\\").init(%s,%s)"' % ('./norm', 3, 5)
# 最后,通过 os.popen 执行命令即可

pipeline = os.popen(cmd)
 
# 读取结果
result = pipeline.read()
 
print('结果是:', result)

 

原因是因为你用requests所获得的源代码是不完整的,然后你在不完整的源代码中查找 class 为 current 的tr元素,没查到,返回给你一个空,对这个空调用 find_all() ,就报了这个错误:'NoneType' object has no attribute 'find_all'

trs=soup.find('tr',class_='current').find_all('tr')

解决方案:参考上面技术专家提供的文章:https://blog.csdn.net/qq_26079939/article/details/114934545,大概的意思就是分析JS、逆向解密,从接口得到真实的数据。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632