장고 모델을 만들고 view 화면에 아래처럼 코드를 작성했습니다.
def scrape(request):
from bs4 import BeautifulSoup
import requests
if request.method == "POST":
keyword = request.POST.get("keyword")
page = request.POST.get("page")
for i in range(1, int(page) + 1):
req = requests.get(
"https://coinpan.com/?error_return_url=%2Ffree&vid=&mid=free&act=IS&is_keyword="
+ keyword
+ "&where=document&page="
+ str(i)
)
soup = BeautifulSoup(req.content, "html.parser")
for i in soup.find_all("a", class_="document_link"):
keyword = i.text
link_address = "https://coinpan.com/" + i.find("a")["href"]
if link_address != "" and keyword is not None:
Keyword.objects.create(
word=keyword, link=link_address,
)
data = Keyword.objects.all()
return render(request, "scrape_result.html", {"data": data})
else:
return render(request, "index.html", {})
실행하면,
위에 < link_address = "https://coinpan.com/" + i.find("a")["href"] >
이 부분이 아래처럼
에러 나옵니다.
" TypeError at /
'NoneType' object is not subscriptable"
자료를 아무리 찾아봐도 문제를 모르겠어요.