liquid_hg 2022-02-16 19:48 采纳率: 100%
浏览 4073
已结题

python报错AttributeError: 'tuple' object has no attribute 'items'

问题遇到的现象和发生背景

写python爬虫时python报错AttributeError: 'tuple' object has no attribute 'items'

问题相关代码,请勿粘贴截图
# --coding=utf-8-- #
import os
import lxml
import requests
from bs4 import BeautifulSoup


# 获取源码并解析
def requester(urls):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/86.0.4240.198 Safari/537.36 '
    }
    res = requests.get(urls, headers=header)
    res.encoding = 'utf-8'
    global soup
    soup = BeautifulSoup(res.content, 'lxml')
    return soup, header


# 寻找图片的url并处理
def seek(soups):
    img_list = soups.find_all('img')
    global src_list
    src_list = []
    for img in img_list:
        src = img.get('src2')
        if src is not None:
            img_url = 'https:' + src
            src_list.append(img_url)
    return src_list


# 保存图片
def save(src_lists, header):
    os.makedirs('E:/image', exist_ok=True)
    for img_url in src_lists:
        img_res = requests.get(img_url, headers=header)
        path = os.path.basename(img_url)
        new_image = open('E:/image/' + path, 'wb')
        print('正在下载:' + img_url + '......')
        new_image.write(img_res.content)
        new_image.close()
        print(img_url + '\n下载完毕')


if __name__ == '__main__':
    url = 'https://sc.chinaz.com/tupian/shugantupian.html'
    soup = requester(url)
    headers = requester(url)
    src_list = seek(soup)
    save(src_list, headers)


运行结果及报错内容
D:\python\python.exe D:/pycharm/jbr/bin/learn_spider/spider_image.py
Traceback (most recent call last):
  File "D:/pycharm/jbr/bin/learn_spider/spider_image.py", line 52, in <module>
    save(src_list, headers)
  File "D:/pycharm/jbr/bin/learn_spider/spider_image.py", line 38, in save
    img_res = requests.get(img_url, headers=header)
  File "D:\python\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "D:\python\lib\site-packages\requests\sessions.py", line 453, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "D:\python\lib\site-packages\requests\models.py", line 319, in prepare
    self.prepare_headers(headers)
  File "D:\python\lib\site-packages\requests\models.py", line 451, in prepare_headers
    for header in headers.items():
AttributeError: 'tuple' object has no attribute 'items'

进程已结束,退出代码1
我的解答思路和尝试过的方法

报错显示的是:属性错误:“tuple”(元组)对象没有属性“items”
但是程序中没有元组啊
但是不写函数又行了?

import lxml
import os
import requests
from bs4 import BeautifulSoup
url = 'https://sc.chinaz.com/tupian/shugantupian.html'
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
res=requests.get(url,headers=headers)
res.encoding='utf-8'
soup=BeautifulSoup(res.content,'lxml')
img_list=soup.find_all('img')
src_list=[]
for img in img_list:
    src=img.get('src2')
    if src!=None:
        img_url='https:'+src
        src_list.append(img_url)
os.makedirs('E:/image', exist_ok=True)
for img_url in src_list:
    img_res=requests.get(img_url,headers=headers)
    path=os.path.basename(img_url)
    new_image = open('E:/image/'+path,'wb')
    print('正在下载:'+img_url+'......')
    new_image.write(img_res.content)
    new_image.close()
    print(img_url+'\n=======================下载完毕=======================')
我想要达到的结果

修改带函数的源码,但效果一样

  • 写回答

4条回答 默认 最新

  • 关注

    def requester(urls): 中 return soup, header 返回两个值, 这两个用逗号分隔的soup, header就会形成一个元组返回,
    就等同 return (soup, header) 元组的()括号有时是可以省略的

    你if name == 'main': 中
    soup = requester(url)
    headers = requester(url)
    改成
    soup, headers = requester(url)

    对返回的元组再进行拆解赋值即可

    if __name__ == '__main__':
        url = 'https://sc.chinaz.com/tupian/shugantupian.html'
        soup, headers = requester(url)
        src_list = seek(soup)
        save(src_list, headers)
    
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月26日
  • 已采纳回答 2月18日
  • 创建了问题 2月16日

悬赏问题

  • ¥20 求文心中文心理分析系统(TextMind)
  • ¥15 chipyard环境搭建问题
  • ¥15 python,pyopenGL的OBV渲染无法设置透明
  • ¥20 matlab可以把多个二维图合成为三维瀑布图吗
  • ¥15 EEPROM,软件i2c
  • ¥500 求解读该段JS代码,需要知道是用干什么的
  • ¥20 qt4代码实现二进制文件读取显示,以及显示的内容进行搜索
  • ¥15 Labview获取LK-G3001数据
  • ¥15 我知道什么是混合树,但是怎么写代码啊
  • ¥50 开发板linux系统安装dpkg,apt函数库 有偿