问题描述
我使用美丽的汤来抓取并成功地从下面描述的网站中提取图像,但是当我查看提取的图像时,它们显示为如下所示的 URL。然后我在尝试将抓取的图像保存到我的 django 数据库时遇到困难,因为下面显示的特定错误不断出现。收到错误后,我还尝试使用 forloop 来创建帖子,因为我想尝试将列表元素保存在单个数据中,但它仍然显示相同的内容,或者我遇到了相同的错误 未提交但是当我从标题、摘要、内容中的文件中删除图像刮取数据并尝试保存到 django 数据库时,它成功了。 保存图像是问题,我需要帮助
下面是我的示例代码
https://people.net/wp-content/uploads/2021/03/ad1-746x375.jpg https://people.net/wp-content/uploads/2020/08/caroon-1-600x375.jpg
这样的东西带来了图像
images = [i['data-src'] for i in soup.find_all('img',{'class','attachment-jnews-750x375 size-jnews-750x375 lazyload wp-post-image'})]
Post.objects.create(
title=title,content_1=paragraphs,image=images,sources=lnk,)
当我尝试保存到模型时会出现错误
class Post(models.Model):
title = models.CharField(max_length=250,blank = True,help_text='Maximum 160 Title characters.')
image = models.FileField(upload_to='images',blank=True,null=True,help_text='You must upload original image before continuing.')
if file and not file._committed: AttributeError: 'list' object has no attribute '_committed'
我尝试使用 for 循环,但也存在同样的错误
for image in images:
/ Post.objects.create(title=title,)
我也遇到了这个错误
if file and not file._committed: AttributeError: 'list' object has no attribute '_committed'
解决方法
请更改您的 for 循环代码片段
for image in images:
Post.objects.create(
title=title,content_1=paragraphs,image=image,sources=lnk,)