I created a long-range model and wrote the code on the view screen as shown below.
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", {})
When executed,
Above < link_address = "https://coinpan.com/" + i.find("a")["href"] >
This part is shown below
There's an error.
" TypeError at /
'NoneType' object is not subscriptable"
I can't find the problem no matter how much I look up the data.
python crawling beautifulsoup
Errors such as object is not writable
occur when:
>>> 0[3]
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
0[3]
TypeError: 'int' object is not subscriptable
>>> a = None
>>> a[3]
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
a[3]
TypeError: 'NoneType' object is not subscriptable
>>> a['ref']
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
a['ref']
TypeError: 'NoneType' object is not subscriptable
In the example above, a
is None
, and an exception occurred in an attempt to access an element in an object with an integer index or string key for this object.
In the code you showed, i.find("a")["href"]
would have returned None
in the i.find("a")["href"]
part.
619 Uncaught (inpromise) Error on Electron: An object could not be cloned
582 PHP ssh2_scp_send fails to send files as intended
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.