Python crawling error. 'NoneType' object is not subscriptable

Asked 2 years ago, Updated 2 years ago, 114 views

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

2022-09-21 11:15

1 Answers

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.


2022-09-21 11:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.