0

I develop django website on cpanel with MySQL database. I have a function that pull feeds from this website https://travelcommunication.net/feed/ and create an object from that (web scraping using beautifulsoup4).

But when I try to grab the content section, the error appears. But that only happens with certain items, not all.

I try on my local (with sqlite database) and all working fine. I have also tried on heroku (with PostgreSQL database) and all working fine.

Here is my code:

#views.py
def pull_feeds(request, pk):
if request.user.is_superuser:
    source = Autoblogging.objects.get(pk=pk)

    url = requests.get(source.url)
    soup = BeautifulSoup(url.content, "html.parser")
    length = source.items
    items = soup.find_all('item')[:length]
    contents = soup.find_all('content:encoded')[:length]

    for i in range(length-1, -1, -1):
        content = contents[i].text
        title = items[i].title.text
        body = content[content.find('<p>'):]  #the problem is here .. when i comment this, everything works fine

        category = Category.objects.get(pk=source.category.id)
        if not Post.objects.filter(title=title).exists():
            post = Post(title=title,
                        body=body, #the problem is here .. when i comment this, everything works fine
                        category=category)

            link = content[content.find('src=')+5:content.find('alt')-2]
            img_data = requests.get(link).content
            with open('temp_image.jpg', 'wb') as handler:
                handler.write(img_data)
            with open('temp_image.jpg', 'rb') as handler:
                file_name = link.split("/")[-1]
                post.cover.save(file_name, files.File(handler))
        os.remove("temp_image.jpg")
    return redirect("news:autoblogging")
else:
    return HttpResponse("Sorry you are not allowed to access this page")

Does anyone know how to fix this error? Thanks.

penk
  • 197
  • 1
  • 3
  • 14
  • Does this answer your question? ["Incorrect string value" when trying to insert UTF-8 into MySQL via JDBC?](https://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc) – Ankit Tiwari Oct 16 '21 at 14:58
  • thanks for the suggestion but I don't understand the solution – penk Oct 16 '21 at 15:06
  • Hello @penk please provide full traceback – Ankit Tiwari Oct 17 '21 at 12:01
  • Hello! Thanks but I've solved it (actually just skip it when the error occurs using try and except). But I have one question. I've read the link you provided, it says you have to change from utf8 to utf8mb4. How to modify mysql database on cpanel? – penk Oct 17 '21 at 13:21

0 Answers0