简易爬虫报错:TypeError: ‘NoneType‘ object is not callable

简易爬虫报错:TypeError: ‘NoneType’ object is not callable

我最近写了一个爬虫,但是它运行到利用bs4中的findall()查找内容时报错TypeError: ‘NoneType’ object is not callable
下面贴上代码给大佬检查一下

#! python3
#-*- coding=utf-8 -*-
#@Author: Bernard
#@Ide: IDLE

import os
import urllib
import urllib.parse
import urllib.request
import re
import sys
import bs4
from bs4 import BeautifulSoup
print('''
      科学技术是第一生产力
KNOWLEDGE IS THE KEY TO THE UNIVERSE
''')


print(os.getcwd())
os.chdir('D:\\My python source code')
print('运行地址已转入D:\\My python source code')
#baseurl_0='https://e621.net/posts?page=' 这是目标url,


def askurl(url_0):
    head_0={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66'}
    #写入伪装user-agent
    data_0=bytes(urllib.parse.urlencode({'N/A':'N/A'}),encoding='utf-8')
    request_0=urllib.request.Request(url=url_0,headers=head_0,data=data_0,method='GET')
    #打包生成访问请求
    html_0='N/A'  #默认返回值
    try:
        response_0=urllib.request.urlopen(request_0,timeout=30)
        html_0=response_0.read().decode('utf-8')  #以utf-8的格式解码网页并保存于内存
    except urllib.error.URLError as char_0:
        if hasattr(char_0,'code'):  #运用hasattr分析报错
            print(char_0.code)
        if hasattr(char_0,'reason'):
            print(char_0.reason)
    return html_0
   

def getdata(baseurl_0):
    datalist_0=[] #生成保存网页数据的列表
    urllist_0=[] #生成保存网址数据的列表
    piclist_0=[] #生成保存图片的列表
    findlink_0=re.compile(r'''<a href="(.*?)">''') #正则表达式提取url
    findlink_1=re.compile(r'''<a href="(.*?)">''')
    findpic_0=re.compile(r'''<a href="(.*?)">''')
    image_name='artist: rainbowscreen'
    num_0=0
    for num_0 in range(0,12): #在该网站的12个页面中爬取数据
        url_0=str(baseurl_0+'?'+'page='+str(num_0+1)+str('&tags=rainbowscreen')) #组合生成目标地址
        print(url_0)
        html_1=askurl(url_0)
        soup_0=BeautifulSoup(html_1,'html.parser')
        sys.stdout.write('1')
        for item_0 in soup_0.find_all('article',class_="post-preview"): #这里有问题
            #定位寻找出预览图片区域
            item_1=str(item_0) #将其转化成字符串形式
            link_1=re.findall(findlink_0,item_1)[0] #提取a herf 前缀下的内容
            link_2=re.findall('/posts/.*',link_1)[0] #提取其中的url后半部分信息
            datalist_0.append(link_2) #存储刚刚提取到的信息
            sys.stdout.write('2') #显示功能,用于错误排查
        for item_2 in datalist_0: #下一步是访问原图地址并保存原图 
            item_3=str(item_2)
            url_1='https://e621.net'+item_3 #组合生成原图下载处地址
            print(url_1)
            html_2=askurl(url_1) #访问原图下载处地址
            soup_1=BeautifulSoup(html_2,'html.parser') #解析网页文件
            sys.stdout.write('3')
            print(soup_1)
            for item_4 in soup_1.findall('a',class_="button btn-warn"):
                item_5=str(item_4)
                link_3=re.findall('''href="(.*?)"''',item_5)
                url_2=re.findall('''^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$''',link_3) #分离出真实图片地址
                urllist_0.append(url_2)
                sys.stdout.write('4')
                for item_6 in urllist_0: #访问原图地址,并下载保存图片
                    url_3=str(item_6)
                    html_3=askurl(url_3)
                    piclist_0.append(html_3)
                    for item_7 in piclist_0:
                        image_name_0=image_name+str(num_0)
                        pic_0=open(image_name+0,'wb') 
                        pic_0.write(item_7)
                        num_0=num_0+1


getdata('https://e621.net/posts') #执行函数
                        在这里插入代码片

这里附上报错截图
在这里插入图片描述
还请大佬过目!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值