gpt4 book ai didi

python - 渲染抛出错误 AttributeError META -(异常位置 : __getattr__ in urllib2)

转载 作者:太空狗 更新时间:2023-10-30 02:12:56 24 4
gpt4 key购买 nike

我关注了this文章尝试从外部网站加载图像。我正在尝试从外部链接中提取所有图像。我已经使用 BeautifulSoup 来解析链接并获取所有必需的链接。

在 View 调用代码末尾的 render() 函数之前,image_list 和 return_dict 具有所需的值。但是,渲染函数似乎正在生成 AttributeError 异常。请协助。

我收到以下错误:

    AttributeError at /post/add_new/
META
Request Method: POST
Request URL: http://localhost:8000/post/add_new/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:
META
Exception Location: C:\Python27\lib\urllib2.py in __getattr__, line 225
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Python Path:
['C:\\Users\\Talal\\Python Workspace\\talal_ynd',
'C:\\Python27\\lib\\site-packages\\ipython-0.13-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pyreadline-2.0_dev1-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\pil-1.1.7-py2.7-win32.egg',
'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
'C:\\windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Thu, 13 Sep 2012 17:30:45 +0500

这是我的 View 文件:

# Create your views here.
from django.shortcuts import render
from django.http import HttpResponseRedirect
from posts.models import Post, PostForm
from django.template.loader import get_template
from talal_ynd.settings import TEMPLATE_DIRS


def post_view(request):
if request.method == 'POST':
post_form = PostForm(request.POST)
if post_form.is_valid():
success_message = 'Thank you.'

link = post_form.cleaned_data['link']
if 'get_link' in request.POST:
import urllib2
request = urllib2.Request(link)
response = urllib2.urlopen(request)
html=response.read()
from BeautifulSoup import BeautifulSoup
soup=BeautifulSoup(html)
import re
title=''; description=''
description=soup.findAll('meta',
attrs={'name':re.compile("description$",
re.I)})[0].get('content')
try:
title=soup.findAll('meta',
attrs={'name':re.compile("^title$",re.I)})[0].get('content')
except:
pass
if not title:
title=soup.title.string
max_images=10
image_tags=soup.findAll('img',limit=max_images)
image_urls_list=[]
image_urls_list2=[]
from urlparse import urljoin
for image_tag in image_tags:
url=image_tag.get('src')
#image_urls_list.append(request.build_absolute_uri(url))#urljoin(link,url))#HttpRequest.build_absolute_uri(url))
#image_urls_list.append(request.urljoin(link,url))
image_urls_list.append(url)
image_list=[]
for url in image_urls_list:
image_list.append({'url':url})
return_dict={'title':title, 'description':description}
return_dict.update({'images':image_list})

else:
post_form = PostForm()
else:

post_form = PostForm()


return render(request, 'posts/post_form.html', locals())

最佳答案

您已经覆盖了函数内的 request 变量,因为您在调用 urllib2.Request 时重用了它。在那里使用不同的变量名称。

关于python - 渲染抛出错误 AttributeError META -(异常位置 : __getattr__ in urllib2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12423581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com