[英]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.