python - Beautifulsoup4 性能树莓派3

标签 python performance web-scraping beautifulsoup kodi

我正在制作一个 Kodi 插件,我将在我的树莓派 pi3 上运行它。 在我的插件中,我从网站上抓取信息,以便我可以填写项目列表。我现在拥有的一切都可以正常工作,但是当我将其部署在树莓派 3 上时,性能就成了问题。网页解析需要15秒

soup = BeautifulSoup(response, "html.parser", parse_only=tiles) << this line

我已经使用 soupstrainer 来提高性能,但这并没有达到我所希望的效果。

    _VRT_BASE = "https://www.vrt.be/"

    def __list_videos_az(self):
    joined_url = urljoin(self._VRTNU_BASE_URL, "./a-z/")
    response = urlopen(joined_url)
    tiles = SoupStrainer('a', {"class": "tile"})
    soup = BeautifulSoup(response, "html.parser", parse_only=tiles)
    listing = []
    for tile in soup.find_all(class_="tile"):
        link_to_video = tile["href"]
        li = self.__get_item(tile, "false")
        url = '{0}?action=getepisodes&video={1}'.format(_url, link_to_video)
        listing.append((url, li, True))

    xbmcplugin.addDirectoryItems(_handle, listing, len(listing))
    xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
    xbmcplugin.endOfDirectory(_handle)

def __get_item(self, element, is_playable):
    thumbnail = self.__format_image_url(element)
    found_element = element.find(class_="tile__title")
    li = None
    if found_element is not None:
        li = xbmcgui.ListItem(found_element.contents[0]
                              .replace("\n", "").strip())
        li.setProperty('IsPlayable', is_playable)
        li.setArt({'thumb': thumbnail})
    return li

有人可以告诉我如何提高程序的性能吗?我想也许正则表达式会更快,但很多人说你不应该以这种方式解析 html,并且将正则表达式放在一起也具有挑战性。

那么我可以做些什么来提高我的表现吗?

最佳答案

我建议尝试lxml parser它是用 C(实际上是 Cython)编写的,通常速度更快。要获取此软件包,请尝试从 Raspbian 安装它(apt-get install python-lxmlpip install lxml),然后将其移至您的插件。 lxml 包包含已编译的二进制模块,因此获取适合您的特定平台的版本非常重要。

关于python - Beautifulsoup4 性能树莓派3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42613923/

相关文章:

Python - 越来越快地运行程序

mysql - 如何加快mysql查询速度

mysql - 如何提高MySQL查询速度

asp.net - runat server 和 default asp label 哪个 Span 更好?

python - 使用 Scrapy 写入多个文件

python - 从 Scrapy 中的管道和中间件关闭数据库连接

python - 程序在 pycharm 中完美运行,在终端中运行时给出错误答案

python - 在 QTConsole 中运行时,IPython 不会重新加载代码

python - 值错误 : dictionary update sequence element #0 has length 1; 2 is required

python - Scrapy处理asp.net站点中的 session cookie或302