django google_link,google_text = google(result)make无法解压缩不可迭代的NoneType对象djanog BeautifulSoup

hiz5n14c  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(140)

我试图使搜索谷歌与BeautifulSoup在socialnetwork django网站项目我下载它作为开源,当我试图使我收到一个错误消息无法解包不可迭代的NoneType对象
那就是search.py
从bs4导入BeautifulSoup导入请求

完成

定义Google(s):链接= []文本= []

USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
headers = {"user-agent": USER_AGENT}
r=None
if r is not None :
    r = requests.get("https://www.google.com/search?q=" + s, headers=headers)
    soup = BeautifulSoup(r.content, "html.parser")
    for g in soup.find_all('div', class_='yuRUbf'):
        a = g.find('a')
        t = g.find('h3')
        links.append(a.get('href'))
        text.append(t.text)   

        return links, text

那就是view.py
延迟结果(请求):如果请求。方法==“POST”:

result = request.POST.get('search')
    
    google_link,google_text = google(result)
    google_data = zip(google_link,google_text)
   

    if result == '':
        return redirect('Home')
    else:
        return render(request,'results.html',{'google': google_data })

这是一个模板

{% for i,j in google  %}
         <a href="{{ i }}" class="btn mt-3 w-100 lg-12 md-12">{{ j }}</a><br>
        
 {% endfor %}

我保留消息无法为google_link,google_text = google解压缩不可迭代的NoneType对象(结果)

avwztpqn

avwztpqn1#

如果对象不相同,not None将返回True。在您的示例中,r=None和**'if r is not None '*将立即被选中,因此返回False。if语句下面的所有行都不涉及。可能是因为以下原因:无法解包不可迭代的NoneType对象。因为该对象不存在。
剩下的代码都可以用了。我修改了一下,如果你的代码不起作用,就用我的。搜索词输入到表单字段中,如果成功保存表单,搜索结果将被转移到“结果”视图中。如果为空,则页面将显示:
*'结果=空'**。
views.py

def google(s):
    USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
    headers = {"user-agent": USER_AGENT}
    r = None
    links = []
    text = []

    r = requests.get("https://www.google.com/search?q=" + s, headers=headers)
    soup = BeautifulSoup(r.content, "html.parser")
    for g in soup.find_all('div', class_='yuRUbf'):
        a = g.find('a')
        t = g.find('h3')
        links.append(a.get('href'))
        text.append(t.text)

    return links, text

def form(request):

    return render(request, 'form.html')

def results(request):
    if request.method == 'POST':
        result = request.POST.get('search')
        google_link, google_text = google(result)
        google_data = zip(google_link, google_text)

        if result == '':
            return HttpResponseNotFound('<h1>result = empty</h1>')
        else:
            return render(request, 'results.html', {'google': google_data})

form.hml

<form method='post' action="{% url 'results' %}">
    {% csrf_token %}
    <p>Name:<br> <input name='search'/></p>
    <input type='submit' value='Send'/>
</form>

results.html

{% for i,j in google  %}
<a href="{{ i }}" class="btn mt-3 w-100 lg-12 md-12">{{ j }}</a><br>
{% endfor %}

urls.py

urlpatterns = [
    path('form/', form, name='form'),
    path('results/', results, name='results'),
]

下面是我的工作方式:

相关问题