为什么这段代码会返回错误 [英] Why does this code return a error
本文介绍了为什么这段代码会返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望从reddit / r / memes /上获取所有图片,并将它们存储在一个文件夹中。
当我运行此代码时,我得到第10行,在< module>
image_tags = soup.findALL('img')
TypeError:'NoneType'对象不可调用
我尝试过:
i want to get all the pictures off of /r/memes/ on reddit and store them in a folder.
when i run this code i get "line 10, in <module>
image_tags = soup.findALL('img')
TypeError: 'NoneType' object is not callable"
What I have tried:
import requests
from bs4 import BeautifulSoup as bs
import os
url = 'https://www.reddit.com/r/memes/'
page = requests.get(url)
soup = bs(page.text,'html.parser')
image_tags = soup.findALL('img')
if not os.path.exists('memes'):
os.makedirs('memes')
os.chdir('memes')
x = 0
for image in image_tags:
try:
url = image['src']
source = requests.get(url)
if source.status_code == 200:
with open('memes-' + str(x) + '.jpg', 'wb') as f:
f.write(requests.get(url).content)
f.close()
x += 1
except:
pass
推荐答案
with open('memes-' + str(x) = '.jpg', 'wb') as f:
那是什么 =
签到那里?
这篇关于为什么这段代码会返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文