栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

爬虫 - 最强解析库selectolax

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬虫 - 最强解析库selectolax

导航
  • 迅捷的解析速度
  • 强大的解析能力


安装一下
pip install selectolax
优势
  • 解析速度快
  • 解析能力强,可解决其它解析库产生的数据残缺问题(少数情况下,一般人遇不着)
劣势
  • 只支持css语法、不支持xpath



迅捷的解析速度,对比1000个文本,selectolax效率约为lxml的3倍
# -*- coding: utf-8 -*-
#  @Author  : markadc

import time
import requests
from lxml import etree
from selectolax.parser import HTMLParser

url = 'https://www.baidu.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71'
}
html = requests.get(url, headers=headers).text


def use_lxml():
    start = time.time()
    for _ in range(1000):
        tree = etree.HTML(html)
        lis = tree.xpath('//ul[@id="hotsearch-content-wrapper"]/li')
    end = time.time()
    print(f'耗时{end - start:.2f}秒  使用lxml')


def use_selectolax():
    start = time.time()
    for _ in range(1000):
        html_parser = HTMLParser(html)
        lis = html_parser.css('ul#hotsearch-content-wrapper > li')
    end = time.time()
    print(f'耗时{end - start:.2f}秒  使用selectolax')


if __name__ == '__main__':
    use_lxml()
    use_selectolax()

上方代码输出结果如下



强大的解析能力

  • 上方图片为爬虫下载到本地的html文件,可以看到用浏览器xpath插件输入规则提取的结果是50个
  • 但是在实际代码中用xpath提取的结果却没有50个,下方为详细代码
# -*- coding: utf-8 -*-
#  @Author  : markadc

from bs4 import BeautifulSoup
from lxml import etree
from selectolax.parser import HTMLParser


with open('./err.html', 'r') as f:
    html = f.read()

# bs4
soup = BeautifulSoup(html, 'lxml')
result = soup.select('div#questions > div')
print(f'得到{len(result)}个div  使用bs4')

# lxml
tree = etree.HTML(html)
result = tree.xpath('//div[@id="questions"]/div')
print(f'得到{len(result)}个div  使用lxml的xpath')
result = tree.cssselect('div#questions > div')
print(f'得到{len(result)}个div  使用lxml的css')

# selectolax
html_parser = HTMLParser(html)
result = html_parser.css('div#questions > div')
print(f'得到{len(result)}个div  使用selectolax的css')

上方代码输出结果如下

  • 可以发现只有selectolax的解析才是正确的50个


总结
  • selectolax又快又强
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026671.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号