![0109cecaf20ac04340f9fab99724886b.png](https://i-blog.csdnimg.cn/blog_migrate/47c9342cf5c50f0c8f18ff6e035015d7.jpeg)
简介
以下介绍的库均为从网页中自动解析想要的内容,从而解放了需要每个网站都要正则匹配或者xpath的超大工作量。
一、lassie:人性化的网页内容检索库
安装
pip3 install lassie
使用:
import lassielassie.fetch('http://www.thepipefittings.com/compression-fittings.html')
输入:
{'images': [{'src': 'http://www.thepipefittings.com/favicon.ico', 'type': 'favicon'}], 'videos': [], 'url': 'http://www.thepipefittings.com/compression-fittings.html', 'title': 'Compression Fittings,Manipulative Compression Fittings,Brass Compression Fittings,Compression Fittings Suppliers', 'status_code': 200}
二、newspaper:新闻内容爬虫专用包
安装:
pip3 install newspaper3k
需要安装的是newspaper3k而不是newspaper,因为newspaper是python 2的安装包,pip install
newspaper 无法正常安装,请用python 3对应的 pip install newspaper3k正确安装。
使用:
from newspaper import Article# import nltk# nltk.download('punkt')url = 'http://www.thepipefittings.com/compression-fittings.html'article = Article(url) # Chinesearticle.download()article.parse()article.nlp()print(article.text)
三、goose3: HTML 内容/文章提取器(python3)
安装:
pip3 install goose3
使用:
from goose3 import Gooseurl = 'http://www.thepipefittings.com/compression-fittings.html'g = Goose()article = g.extract(url=url)article.title# article.meta_description# article.cleaned_text[:]
输入:
'Compression Fittings,Manipulative Compression Fittings,Brass Compression Fittings,Compression Fittings Suppliers'
四、python-readability:arc90 公司 readability 工具的 Python 高速端口
安装:
pip3 install readability-lxml
使用:
import requestsfrom readability import Document url = 'https://www.pipingengineer.org/piping-materials-buttweld-fittings/'html = requests.get(url).contentdoc = Document(html)print('title:', doc.title())print('content:', doc.summary(html_partial=True))
输出:
title: Not Acceptable!content:
Not Acceptable!
An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.
五、textract:从任何格式的文档中提取文本,Word,PowerPoint,PDFs 等等
安装
pip3 install textract
使用:
import textracttext = textract.process("xxx.pdf") #换成你自己本地的pdfprint(text.decode('utf-8'))