简体   繁体   English

AttributeError:当我保存在age中时,“ NoneType”对象没有属性“ get”

[英]AttributeError: 'NoneType' object has no attribute 'get' when I save in amage

I want to save the picture from site, but when i`m trying to print 我想从站点保存图片,但是当我尝试打印时

pict_link

the following error has occured. 发生以下错误。 Because of this, the whole loop stops and the program stops working. 因此,整个循环停止并且程序停止工作。 I tried to work through [.attrs] , but this also did not help. 我尝试通过[.attrs]进行工作,但这也没有帮助。 I need your help. 我需要你的帮助。

import sys, os, requests, datetime, time
from bs4 import BeautifulSoup
import urllib.request
import selenium
from selenium import webdriver

def get_html(url):
    headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}
    r = requests.get(url, headers=headers)
    return r.content

def pictures(pict_link, file):
      img = requests.get(pict_link) 
      out = open(file, "wb") 
      out.write(img.content) 
      out.close()


link = 'https://www.clubx.com.au/products/womanizer-pro?variant=37834367948'
soup = BeautifulSoup(get_html(link), 'html.parser')
browser = webdriver.PhantomJS('./phantomjs/bin/phantomjs')
browser.get(link)
soup = BeautifulSoup(browser.page_source, 'lxml')
box = soup.find('div',{'class':'swatch clearfix'})
rows = box.find_all('div')[1:]
nn = 1
for row in rows:
    pict_link = row.find('img').get('src')
    print(pict_link)
    codice = link.split('/products/')[1].split('?variant')[0] + '_' + str(nn)
    foto = './Pictures/' + codice + '.jpg'
    name_foto = codice + '.jpg'
    pictures(pict_link, foto)
    label = 'pict_' + str(nn)
    nn += 1
browser.quit()

Documentation ( https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find ) is clear about this: 文档( https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find )对此很清楚:

If find() can't find anything, it returns None 如果find()找不到任何东西,则返回None

Change 更改

pict_link = row.find('img').get('src')

to something like

pict = row.find('img')
if pict is None:
    continue
pict_link = pict.get('src')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:'NoneType'对象没有属性'save' - AttributeError: 'NoneType' object has no attribute 'save' 为什么我会收到“AttributeError: 'NoneType' object has no attribute 'get'” - why am i getting "AttributeError: 'NoneType' object has no attribute 'get' " AttributeError: 'NoneType' object 在写入文件时没有属性 'get' - AttributeError: 'NoneType' object has no attribute 'get' when writing into a file Flasgger AttributeError:'NoneType'对象没有属性'get'吗? - Flasgger AttributeError: 'NoneType' object has no attribute 'get'? 我收到此错误 --> AttributeError: 'NoneType' object has no attribute 'predict' - I get this error --> AttributeError: 'NoneType' object has no attribute 'predict' 为什么会出现 AttributeError: 'NoneType' object has no attribute 'something'? - Why do I get AttributeError: 'NoneType' object has no attribute 'something'? 为什么我得到 AttributeError: 'NoneType' object has no attribute 'email' - Why do I get AttributeError: 'NoneType' object has no attribute 'email' 为什么会出现 AttributeError: 'NoneType' object has no attribute 'attrs'? - Why do I get AttributeError: 'NoneType' object has no attribute 'attrs'? 我得到的错误是 AttributeError: 'NoneType' object has no attribute 'date' - The error that I get is AttributeError: 'NoneType' object has no attribute 'date' 尝试将 append 加入列表时,出现错误“AttributeError: 'NoneType' object has no attribute 'append'” - When trying to append to a list, I get the error "AttributeError: 'NoneType' object has no attribute 'append'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM