Reputation: 41
hi everybody I make in my project a search on google with beautifulsoup and I received this message can only concatenate str (not "NoneType") to str when I try to search this is search.py
from django.shortcuts import render, redirect
import requests
from bs4 import BeautifulSoup
# done
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
and this is views.py
from django.shortcuts import render, redirect
from netsurfers.search import google
from bs4 import BeautifulSoup
def home(request):
return render(request,'home.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 redirect('home')
else:
return render(request,'results.html',{'google': google_data})
and this is urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home,name='home'),
path('results/',views.results,name='Result')
]
and this is a template home
<form method='post' action="{% url 'Result' %}" class="d-flex" role="search">
{% csrf_token %}
<input class="form-control me-2 " type="search" placeholder="ابحث وشارك بحثك مع الاخرين" aria-label="Search" style="width:22rem;">
<input type="submit" class="btn btn-outline-success" value="ابحث" >
</form>
and this is the template results
{% for i,j in google %}
<a href="{{ i }}" class="btn mt-3 w-100">{{ j }}</a><br>
{% endfor %}
I try to search with google with BeautifulSoup library but I got this message instead can only concatenate str (not "NoneType") to str
Upvotes: 0
Views: 167
Reputation: 91
your input template should have name property
<input class="form-control me-2 " type="search" placeholder="ابحث وشارك بحثك مع الاخرين" aria-label="Search" style="width:22rem;" name="search">
Upvotes: 1