ImportError: No module named bs4
#1
Trying to create my first addon,

I have wrote the code and tested with Python and all is good. I've created my addon addon.xml and main.py wrote my code inside. I have inported "script.module.beautifulsoup4" in my addon.xml but when launching my add I get the error -
python:
Error Type: <type 'exceptions.ImportError'>
Error Contents: No module named bs4
Traceback (most recent call last):
File "C:\Users\hp\AppData\Roaming\Kodi\addons\io.test.test\main.py", line 10, in <module>
from bs4 import BeautifulSoup
 ImportError: No module named bs4

My addon code is as follows - 

python:
# -*- coding: utf-8 -*-

from resources.lib import kodilogging
from resources.lib import script

import logging
import xbmcaddon

import requests
from bs4 import BeautifulSoup

r = requests.get('MYURLHERE')
soup = BeautifulSoup(r.text, "html.parser")

for row in soup.find('ul', id='myUL').find_all('li'):
    print(row.text)


Kind of struggling here if anyone can help me.
Thanks.
Reply
#2
Full debug log?
Reply
#3
Quote:15:43:24.486 T:2476  NOTICE: special://profile/ is mapped to: special://masterprofile/
15:43:24.486 T:2476  NOTICE: -----------------------------------------------------------------------
15:43:24.486 T:2476  NOTICE: Starting Kodi (17.6 Git:20171114-a9a7a20). Platform: Windows NT x86 32-bit
15:43:24.486 T:2476  NOTICE: Using Release Kodi x32 build
15:43:24.486 T:2476  NOTICE: Kodi compiled Dec 10 2017 by MSVC 190024215 for Windows NT x86 32-bit version 10.0 (0x0A000000)
15:43:24.486 T:2476  NOTICE: Running on Hewlett-Packard HP Pavilion g6 Notebook PC with Windows 10, kernel: Windows NT x86 64-bit version 10.0
15:43:24.486 T:2476  NOTICE: FFmpeg version/source: ffmpeg-3.1-kodi
15:43:24.487 T:2476  NOTICE: Host CPU: Unknown, 4 cores available
15:43:24.487 T:2476  NOTICE: Desktop Resolution: 1366x768 32Bit at 60Hz
15:43:24.487 T:2476  NOTICE: Running with restricted rights
15:43:24.488 T:2476  NOTICE: Aero is enabled
15:43:24.488 T:2476  NOTICE: special://xbmc/ is mapped to: C:\Program Files (x86)\Kodi
15:43:24.488 T:2476  NOTICE: special://xbmcbin/ is mapped to: C:\Program Files (x86)\Kodi
15:43:24.488 T:2476  NOTICE: special://xbmcbinaddons/ is mapped to: C:\Program Files (x86)\Kodi/addons
15:43:24.488 T:2476  NOTICE: special://masterprofile/ is mapped to: C:\Users\hp\AppData\Roaming\Kodi\userdata
15:43:24.488 T:2476  NOTICE: special://home/ is mapped to: C:\Users\hp\AppData\Roaming\Kodi\
15:43:24.488 T:2476  NOTICE: special://temp/ is mapped to: C:\Users\hp\AppData\Roaming\Kodi\cache
15:43:24.488 T:2476  NOTICE: special://logpath/ is mapped to: C:\Users\hp\AppData\Roaming\Kodi\
15:43:24.488 T:2476  NOTICE: The executable running is: C:\Program Files (x86)\Kodi\kodi.exe
15:43:24.492 T:2476  NOTICE: Local hostname: HP
15:43:24.492 T:2476  NOTICE: Log File is located: C:\Users\hp\AppData\Roaming\Kodi\/kodi.log
15:43:24.492 T:2476  NOTICE: -----------------------------------------------------------------------
15:43:24.496 T:2476  NOTICE: load settings...
15:43:24.506 T:2476 WARNING: CSettingString: unknown options filler "timezonecountries" of "locale.timezonecountry"
15:43:24.506 T:2476 WARNING: CSettingString: unknown options filler "timezones" of "locale.timezone"
15:43:24.534 T:2476  NOTICE: No settings file to load (special://xbmc/system/advancedsettings.xml)
15:43:24.535 T:2476  NOTICE: No settings file to load (special://masterprofile/advancedsettings.xml)
15:43:24.535 T:2476  NOTICE: Default Video Player: VideoPlayer
15:43:24.535 T:2476  NOTICE: Default Audio Player: paplayer
15:43:24.535 T:2476  NOTICE: Disabled debug logging due to GUI setting. Level 0.
15:43:24.535 T:2476  NOTICE: Log level changed to "LOG_LEVEL_NORMAL"
15:43:24.535 T:2476  NOTICE: CMediaSourceSettings: loading media sources from special://masterprofile/sources.xml
15:43:24.537 T:2476  NOTICE: Loading player core factory settings from special://xbmc/system/playercorefactory.xml.
15:43:24.541 T:2476  NOTICE: Loaded playercorefactory configuration
15:43:24.541 T:2476  NOTICE: Loading player core factory settings from special://masterprofile/playercorefactory.xml.
15:43:24.541 T:2476  NOTICE: special://masterprofile/playercorefactory.xml does not exist. Skipping.
15:43:24.559 T:2476  NOTICE: Running database version Addons27
15:43:24.919 T:2476  NOTICE: ADDONS: Using repository repository.xbmc.org
15:43:24.947 T:7916  NOTICE: Found 2 Lists of Devices
15:43:24.947 T:7916  NOTICE: Enumerated DIRECTSOUND devices:
15:43:24.947 T:7916  NOTICE:     Device 1
15:43:24.947 T:7916  NOTICE:         m_deviceName      : {1609A4E1-04D8-4E12-AFF4-3E4281846979}
15:43:24.947 T:7916  NOTICE:         m_displayName     : Speakers - Speakers (2- High Definition Audio Device)
15:43:24.947 T:7916  NOTICE:         m_displayNameExtra: DIRECTSOUND: Speakers (2- High Definition Audio Device)
15:43:24.947 T:7916  NOTICE:         m_deviceType      : AE_DEVTYPE_PCM
15:43:24.947 T:7916  NOTICE:         m_channels        : FL,FR
15:43:24.947 T:7916  NOTICE:         m_sampleRates     : 48000
15:43:24.947 T:7916  NOTICE:         m_dataFormats     : AE_FMT_FLOAT
15:43:24.947 T:7916  NOTICE:         m_streamTypes     : No passthrough capabilities
15:43:24.947 T:7916  NOTICE:     Device 2
15:43:24.947 T:7916  NOTICE:         m_deviceName      : default
15:43:24.947 T:7916  NOTICE:         m_displayName     : default
15:43:24.947 T:7916  NOTICE:         m_displayNameExtra:
15:43:24.947 T:7916  NOTICE:         m_deviceType      : AE_DEVTYPE_PCM
15:43:24.947 T:7916  NOTICE:         m_channels        : FL,FR
15:43:24.947 T:7916  NOTICE:         m_sampleRates     : 48000
15:43:24.947 T:7916  NOTICE:         m_dataFormats     : AE_FMT_FLOAT
15:43:24.947 T:7916  NOTICE:         m_streamTypes     : No passthrough capabilities
15:43:24.947 T:7916  NOTICE: Enumerated WASAPI devices:
15:43:24.947 T:7916  NOTICE:     Device 1
15:43:24.947 T:7916  NOTICE:         m_deviceName      : {1609A4E1-04D8-4E12-AFF4-3E4281846979}
15:43:24.947 T:7916  NOTICE:         m_displayName     : Speakers - Speakers (2- High Definition Audio Device)
15:43:24.947 T:7916  NOTICE:         m_displayNameExtra: WASAPI: Speakers (2- High Definition Audio Device)
15:43:24.947 T:7916  NOTICE:         m_deviceType      : AE_DEVTYPE_PCM
15:43:24.947 T:7916  NOTICE:         m_channels        : FL,FR
15:43:24.947 T:7916  NOTICE:         m_sampleRates     : 192000,96000,88200,48000,44100
15:43:24.947 T:7916  NOTICE:         m_dataFormats     : AE_FMT_S24NE4MSB,AE_FMT_S16NE,AE_FMT_S16LE,AE_FMT_S16BE
15:43:24.947 T:7916  NOTICE:         m_streamTypes     : No passthrough capabilities
15:43:24.947 T:7916  NOTICE:     Device 2
15:43:24.947 T:7916  NOTICE:         m_deviceName      : default
15:43:24.947 T:7916  NOTICE:         m_displayName     : default
15:43:24.947 T:7916  NOTICE:         m_displayNameExtra:
15:43:24.947 T:7916  NOTICE:         m_deviceType      : AE_DEVTYPE_PCM
15:43:24.947 T:7916  NOTICE:         m_channels        : FL,FR
15:43:24.947 T:7916  NOTICE:         m_sampleRates     : 192000,96000,88200,48000,44100
15:43:24.947 T:7916  NOTICE:         m_dataFormats     : AE_FMT_S24NE4MSB,AE_FMT_S16NE,AE_FMT_S16LE,AE_FMT_S16BE
15:43:24.947 T:7916  NOTICE:         m_streamTypes     : No passthrough capabilities
15:43:25.107 T:2476  NOTICE: Found screen: Generic PnP Monitor on Intel® HD Graphics 3000, adapter 0.
15:43:25.107 T:2476  NOTICE: Primary mode: 1366x768@ 60.00 - Full Screen
15:43:25.107 T:2476  NOTICE: Additional mode: 320x200@ 60.00 - Full Screen
15:43:25.108 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.108 T:2476  NOTICE: Additional mode: 320x240@ 60.00 - Full Screen
15:43:25.108 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.108 T:2476  NOTICE: Additional mode: 400x300@ 60.00 - Full Screen
15:43:25.108 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.108 T:2476  NOTICE: Additional mode: 512x384@ 60.00 - Full Screen
15:43:25.109 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.109 T:2476  NOTICE: Additional mode: 640x400@ 60.00 - Full Screen
15:43:25.109 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.109 T:2476  NOTICE: Additional mode: 640x480@ 60.00 - Full Screen
15:43:25.109 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.109 T:2476  NOTICE: Additional mode: 800x600@ 60.00 - Full Screen
15:43:25.110 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.110 T:2476  NOTICE: Additional mode: 1024x768@ 60.00 - Full Screen
15:43:25.110 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.110 T:2476  NOTICE: Additional mode: 1280x600@ 60.00 - Full Screen
15:43:25.110 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.110 T:2476  NOTICE: Additional mode: 1280x720@ 60.00 - Full Screen
15:43:25.110 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.110 T:2476  NOTICE: Additional mode: 1280x768@ 60.00 - Full Screen
15:43:25.111 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.111 T:2476  NOTICE: Additional mode: 1360x768@ 60.00 - Full Screen
15:43:25.111 T:2476  NOTICE: Previous line repeats 2 times.
15:43:25.111 T:2476  NOTICE: Additional mode: 1366x768@ 60.00 - Full Screen
15:43:25.111 T:2476  NOTICE: Checking resolution 15
15:43:25.944 T:8388  NOTICE: Running database version Addons27
15:43:25.945 T:8388  NOTICE: Running database version ViewModes6
15:43:25.947 T:8388  NOTICE: Running database version Textures13
15:43:25.949 T:8388  NOTICE: Running database version MyMusic60
15:43:25.952 T:8388  NOTICE: Running database version MyVideos107
15:43:25.954 T:8388  NOTICE: Running database version TV29
15:43:25.956 T:8388  NOTICE: Running database version Epg11
15:43:26.696 T:2476 WARNING: JSONRPC: Could not parse type "Setting.Details.SettingList"
15:43:26.748 T:2476 WARNING: CApplication::Initialize - startup.xml taints init process
15:43:26.777 T:2476  NOTICE: initialize done
15:43:26.777 T:2476  NOTICE: Running the application...
15:43:26.853 T:8388  NOTICE: AddOnLog: PVR IPTV Simple Client: Loaded 269 channels.
15:43:26.871 T:2476  NOTICE: starting upnp client
15:43:26.876 T:7276  NOTICE: ES: Starting UDP Event server on port 9777
15:43:26.876 T:7276  NOTICE: UDP: Listening on port 9777 (ipv6 : false)
15:43:27.887 T:10232  NOTICE: EPG::CEpgContainer::Start - EPG thread started
15:43:29.209 T:5788  NOTICE: script.tv.show.next.aired: ### TV Show - Next Aired starting background proc (6.0.15)
15:43:29.241 T:5788  NOTICE: script.tv.show.next.aired: ### no prior data found
15:43:29.562 T:3728  NOTICE: AddOnLog: PVR IPTV Simple Client: EPG Loaded.
15:43:31.407 T:5788  NOTICE: script.tv.show.next.aired: ### starting data update
15:43:37.663 T:4752  NOTICE: Skin Helper Widgets --> Backgroundservice started
15:43:37.801 T:8940  NOTICE: [plugin.video.youtube] Startup: detected Krypton (Kodi-17.6), DASH_SUPPORT_ADDON = True
15:43:37.831 T:1800  NOTICE: Skin Helper Service --> WebService - start helper webservice on port 52307
15:43:37.831 T:3288  NOTICE: Skin Helper Service --> Skin Helper Service version 1.1.3 started
15:43:39.912 T:452   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <type 'exceptions.ImportError'>
                                            Error Contents: No module named bs4
                                            Traceback (most recent call last):
                                              File "C:\Users\hp\AppData\Roaming\Kodi\addons\io.test.test\main.py", line 10, in <module>
                                                from bs4 import BeautifulSoup
                                            ImportError: No module named bs4
                                            -->End of Python script error report<--
Reply
#4
could be that the bs4 module isn't enabled in kodi, so doublecheck that.
Settings > System > Addons > Manage dependencies
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#5
Genius.

Now getting, 

python:
Error Type: <type 'exceptions.UnicodeEncodeError'>
Error Contents: 'ascii' codec can't encode character u'\xe9' in position 24: ordinal not in range(128)
Traceback (most recent call last):
File "C:\Users\hp\AppData\Roaming\Kodi\addons\io.test.test\main.py", line 16, in <module>
print(row.text)
File "<string>", line 7, in write
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 24: ordinal not in range(128)
Reply
#6
get used to it :-)

tips and tricks: https://forum.kodi.tv/showthread.php?tid=144677
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#7
Already am, you seem to be everywhere whenever i'm browsing for help  ;-)

I followed what you posted and fixed that problem, but as usual another comes along-

python:
print(row.text)
IOError: (9, 'Bad file descriptor')

The script is working in Python, but not Kodi. Right now for testing I am simply opening the addon from within Kodi. This is my complete .py file. Can you see anything wrong?

python:

# -*- coding: utf-8 -*-

from resources.lib import kodilogging
from resources.lib import script

import xbmcgui
import xbmcaddon
import logging
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import requests
from bs4 import BeautifulSoup

r = requests.get('MYURL')
soup = BeautifulSoup(r.text, "html.parser")

for row in soup.find('ul', id='myUL').find_all('li'):
    print(row.text)

I'm simply wanting to open my addon and display the text that BSoup has scraped for now. Then I can make changes another day.
Also looked around and there's not much on the error I'm getting and I've tried a few things to no avail.

Thanks.
Reply
#8
no idea about that one..

you could try to add import xbmc and use xbmc.log() instead of print()
not sure if it make a difference, just a wild guess.
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#9
(2018-02-20, 20:20)Daveabuk Wrote: r = requests.get('MYURL')
soup = BeautifulSoup(r.text, "html.parser")

 r = requests.get('MYURL').decode('utf-8' , 'ignore')
Reply
#10
(2018-02-23, 18:07)antrrax Wrote: r = requests.get('MYURL').decode('utf-8' , 'ignore')

Bad advice. requests.get returns response object that is not a string. Page content as a decoded Unicode string is obtained via response.text property and if you need a raw byte string for some reason, there's response.content property.
Reply
#11
Quote:Bad advice. requests.get returns response object that is not a string. Page content as a decoded Unicode string is obtained via response.text property and if you need a raw byte string for some reason, there's response.content property.
I did not pay attention to this detail.


----------

It's just that I had similar problems like accentuation and BeautifulSoup. The solution I found was this:
Quote:#!/usr/bin/env python
# -*- coding: cp1252 -*-

import xbmc, xbmcplugin, xbmcgui, xbmc, xbmcaddon
import urllib, urllib2, re, HTMLParser, os
import html5lib
from bs4 import BeautifulSoup
import requests

MY_HEADER = 'MyAddonName'

def msg(text):
    dialog = xbmcgui.Dialog()
    ok = dialog.ok(MY_HEADER, text)
    
def log(text):
    xbmc.log('[***** DEBUG - %s]: %s' %(MY_HEADER, text), xbmc.LOGNOTICE)

def open_url(url, H=''):
    pg_source = ''
    req = urllib2.Request(url)
    req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36')
    if H != '': req.add_header('Referer', H)
    try:
        response = urllib2.urlopen(req)
        pg_source = response.read().decode('utf-8' , 'ignore')
        response.close()
    except urllib2.URLError, e:
        if hasattr(e, 'code'):
            try:
                msg('HTTP erro nº: ' + str(e.code) + ' \n' + str(e.reason) + '\n' + e.url)
                log('HTTP erro nº: ' + str(e.code) + ' \n' + str(e.reason) + '\n' + e.url)
            except:
                msg('HTTP erro nº: ' + str(e.code) + '\n' + e.url)
                log('HTTP erro nº: ' + str(e.code) + '\n' + e.url)
        elif hasattr(e, 'reason'):
            msg(str(e.reason))
            log(str(e.reason))
    return pg_source


def ViewBS(url):
    content =
    try:
        page_source = open_url(url)
        soup = BeautifulSoup(page_source)
        content = BeautifulSoup(soup.find('div', { 'class' : 'filmes' }).prettify())
    except:
        pass

    log(str(content))

ViewBS('MYURL')
Reply
#12
Thanks for the help!

After trying:
Quote:r = requests.get('MYURL').decode('utf-8' , 'ignore')

I'm now getting:
Quote:IndentationError: ('unexpected indent', ('C:\\Users\\hp\\AppData\\Roaming\\Kodi\\addons\\io.test.test\\main.py', 15, 2, "\t r = requests.get('MYURL').decode('utf-8' , 'ignore')\n"))
Reply
#13
well the error says it all. don't mix spaces and tabs for indenting
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#14
Sorry I'm extremely new and still learning. I'll check my code.
Reply
#15
Ok, I think I've fixed my indents. This is now my full code:
Quote:from resources.lib import kodilogging
from resources.lib import script
import xbmcgui
import xbmcaddon
import logging
import sys
import requests
from bs4 import BeautifulSoup
r = requests.get('MYURL').decode('utf-8' , 'ignore')
soup = BeautifulSoup(r.text, "html.parser")

for row in soup.find('ul', id='myUL').find_all('li'):
    xbmc.log()
I'm now left with this,
Quote:r = requests.get('MYURL').decode('utf-8' , 'ignore')
                                            AttributeError: 'Response' object has no attribute 'decode'
Reply

Logout Mark Read Team Forum Stats Members Help
ImportError: No module named bs40