使用readability-lxml 提取网页标题和主体内容 - 尝试

python-readability

Given a html document, it pulls out the main body text and cleans it up.
This is a python port of a ruby port of arc90’s readability project.

python-readability可以获取一个HTML文献的主体内容:

gIthub:https://github.com/buriy/python-readability
pypi:https://pypi.org/project/readability-lxml/

Installation

:pip3 install readability-lxml
Collecting readability-lxml
  Downloading https://files.pythonhosted.org/packages/af/a7/8ea52b2d3de4a95c3ed8255077618435546386e35af8969744c0fa82d0d6/readability-lxml-0.7.1.tar.gz
Collecting chardet
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
     |████████████████████████████████| 143kB 412kB/s 
Requirement already satisfied: lxml in /usr/local/lib/python3.7/site-packages (from readability-lxml) (4.5.0)
Requirement already satisfied: cssselect in /usr/local/lib/python3.7/site-packages (from readability-lxml) (1.1.0)
Building wheels for collected packages: readability-lxml
  Building wheel for readability-lxml (setup.py) ... done
  Created wheel for readability-lxml: filename=readability_lxml-0.7.1-cp37-none-any.whl size=16481 sha256=acfc6743aa167f8db937221c4a5b033352524d70d7cde13bdb98724b66467a74
  Stored in directory: /Users/lurongming/Library/Caches/pip/wheels/94/48/e5/d944e616d8b0734c3b9cf30a21f4afcf855a1e2b85f82f34fb
Successfully built readability-lxml
Installing collected packages: chardet, readability-lxml
Successfully installed chardet-3.0.4 readability-lxml-0.7.1
Example

需要安装requests:

终端输入:pip install requests

requests的文档:http://cn.python-requests.org/zh_CN/latest/

全部语句如下:

import requests
from readability import Document
response = requests.get('http://acm.hnucm.edu.cn/JudgeOnline/problemset.php')
doc = Document(response.text)
doc.title()
doc.summary()
:python3
Python 3.7.6 (default, Feb 16 2020, 17:48:02) 
[Clang 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> from readability import Document
>>> response = requests.get('http://acm.hnucm.edu.cn/JudgeOnline/problemset.php')
>>> doc = Document(response.text)
>>> doc.title()
'HNUCM-OJ'
>>> doc.summary()
'<html><body><div><tbody>\n\t\。。。

不太清晰。解析的有问题。

再看看。

发布了140 篇原创文章 · 获赞 6 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/LU_ZHAO/article/details/104858805