前言
嗨嗨大家晚上好!今天给大家分享一篇文章,来教你们对比python中的各种数据解析库的速度
本文以一个小说网站页面为例,提取里面的网址和各章节的名称
将各种数据库解析的速度进行对比,对比对象以下4种方式:
1>>>正则表达式
2>>>BeautifulSoup(忽略其中的css选择器需调用的select方法)
3>>>parsel(包含使用xpath提取和css选择器提取)
4>>>lxml库etree模块XPath
对比速度时, >表示 速度快于 >>表示速度远快于 =表示速度差不多
操作思路:
1>>>下载一个小说网页后保存为html格式文件,
2>>>依次用不同的解析数据工具提取所需数据并记录耗时.
3>>>模拟对提取到的数据(url和章节名称)进行字符串操作(网址拼接,章节名替换之类的),记录耗时
4>>>将步骤2的耗时数据与步骤3的耗时数据依次对比
代码如下
需要源码、教程,或者是自己有关python不懂的问题,都可以来这里哦 https://jq.qq.com/?_wv=1027&k=rdIEl7BP 这里还有学习资料与免费课程领取
import time
import re
from bs4 import BeautifulSoup
from parsel import Selector
from lxml import etree
def fnc(): #正则提取
with open("d:/爬虫/1.html",'r',encoding='utf-8') as f:
href = re.findall(r'<dd><a href="(.*?)">(第.*?)</a>',f.read(),re.S)
for i in href:
href = i[0]
chapter = i[1]
def fnc1(): #parser库css提取
with open("d:/爬虫/1.html",'r',encoding='utf-8') as f:
se = Selector(f.read())
chapter = se.css('.box_con a::text').getall()[16:]
href = se.css('.box_con a::attr(href)').getall()[22:]
# href = se.css('.box_con a').re('href="(.*?)">(第.*?)</a>')
# css结合正则会更加耗时,大约是原来耗时的双倍
def fnc2(): #parser库xpath提取
with open("d:/爬虫/1.html"