ランキングに戻る

GeneralNewsExtractor/GeneralNewsExtractor

Pythongne.kingname.info

新闻网页正文通用抽取器 Beta 版.

python3webcrawlerwebspider
スター成長
スター
3.8k
フォーク
540
週間成長
Issue
13
1k2k3k
2023年1月2024年3月2025年5月2026年7月
成果物PyPIpip install generalnewsextractor
README

GNE (GeneralNewsExtractor)

中文文档

Star History

Star History Chart

Introduction

GNE is a general-purpose news content extractor built in Python. It is based on the algorithm described in the paper "Web Content Extraction Based on Text and Symbol Density".

The algorithm is clean, logical, and effective. Since the paper only describes the algorithm without a concrete implementation, this project implements it in Python. It has been tested on major Chinese news sites (Toutiao, NetEase News, Sina News, iFeng, Tencent News, ReadHub, etc.) with nearly 100% accuracy.

Current Status

Beyond the body text extraction described in the paper, GNE also supports automatic detection and extraction of title, publish time, and author.

This project is named "Extractor" rather than "Crawler" by design — the input is HTML, and the output is a dictionary. You are responsible for obtaining the HTML of target pages using your own methods.

This project does not and will not provide any functionality to actively request HTML from websites.

Usage

Online Demo

You can try GNE online at http://gne.kingname.info/. Simply paste the rendered HTML into the text area and click the extract button. For more precise extraction, additional parameters can be provided. See the API documentation for details.

Installation

# Install via pip
pip install --upgrade gne

# Or install via pipenv
pipenv install gne

Basic Usage

Extract Article Content

from gne import GeneralNewsExtractor

html = '''Your rendered HTML code'''

extractor = GeneralNewsExtractor()
result = extractor.extract(html, noise_node_list=['//div[@class="comment-list"]'])
print(result)

# Output:
# {"title": "xxxx", "publish_time": "2019-09-10 11:12:13", "author": "yyy", "content": "zzzz", "images": ["/xxx.jpg", "/yyy.png"]}

For more details, see the GNE documentation.

Extract List Pages (Beta)

from gne import ListPageExtractor

html = '''Your rendered HTML code'''
list_extractor = ListPageExtractor()
result = list_extractor.extract(html, feature='XPath of any element in the list')
print(result)

Advanced Usage

Custom Title XPath

If automatic title extraction fails, you can specify a custom XPath:

from gne import GeneralNewsExtractor

extractor = GeneralNewsExtractor()
html = 'Your target page HTML'
result = extractor.extract(html, title_xpath='//h5/text()')
print(result)

Noise Removal

Some news pages contain comments that may look more like body text than the actual article. Use the noise_node_list parameter to remove interfering elements before extraction:

result = extractor.extract(html, noise_node_list=['//div[@class="comment-list"]'])

noise_node_list accepts a list of XPath expressions, each targeting an element to be removed during preprocessing.

Development Setup

git clone https://github.com/kingname/GeneralNewsExtractor.git
cd GeneralNewsExtractor
pipenv install
pipenv shell
python3 example.py

Notes

  • The example.py file provides basic usage examples.
  • Test code is located in the tests directory.
  • The input HTML must be JavaScript-rendered HTML, not raw page source. This means GNE works with both server-side rendered and Ajax-loaded content.
  • To manually test a new page, open it in Chrome, go to Developer Tools, locate the <html> tag in the Elements tab, right-click and select Copy > Copy OuterHTML.

  • You can also use Puppeteer/Pyppeteer, Selenium, or any other method to obtain the JavaScript-rendered source code.
  • List page extraction is an experimental feature and should not be used in production. You can use Chrome DevTools' Copy XPath to copy the XPath of any item in the list. GNE will automatically find other items in the same list.

Screenshots

NetEase News

Toutiao

Sina News

iFeng

NetEase News List Page

Documentation

GNE FAQ & Q&A

Known Issues

  1. GNE is designed for news article pages. It may not work well on non-news pages or photo gallery articles.
  2. The author field may be empty if the article does not specify an author or if the author pattern is not covered by the existing regular expressions.

Todo

  • Use a configuration file for constants instead of hard-coding them.
  • Allow custom patterns for time and author extraction.
  • News article list page extraction.
  • Support multi-page articles by accepting a list of HTMLs and concatenating the extracted content.
  • Optimize extraction speed.
  • Test on more news websites.
  • ...

Community

Acknowledgements

@bigbrother666sh

関連リポジトリ
fastapi/fastapi

FastAPI framework, high performance, easy to learn, fast to code, ready for production

PythonPyPIMIT Licensepythonjson
fastapi.tiangolo.com
100.8k9.7k
sherlock-project/sherlock

Hunt down social media accounts by username across social networks

PythonPyPIMIT Licenseosintreconnaissance
sherlockproject.xyz
86.9k10.2k
666ghj/MiroFish

A Simple and Universal Swarm Intelligence Engine, Predicting Anything. 简洁通用的群体智能引擎,预测万物

PythonPyPIGNU Affero General Public License v3.0agent-memoryfinancial-forecasting
mirofish.ai
69.1k10.8k
Asabeneh/30-Days-Of-Python

The 30 Days of Python programming challenge is a step-by-step guide to learn the Python programming language in 30 days. This challenge may take more than 100 days. Follow your own pace. These videos may help too: https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw

PythonPyPI30-days-of-pythonpython
68.8k12.8k
Textualize/rich

Rich is a Python library for rich text and beautiful formatting in the terminal.

PythonPyPIMIT Licensepythonpython3
rich.readthedocs.io/en/latest/
56.9k2.3k
astral-sh/ruff

An extremely fast Python linter and code formatter, written in Rust.

Rustcrates.ioMIT Licenselinterpep8
docs.astral.sh/ruff
48.8k2.3k
666ghj/BettaFish

微舆:人人可用的多Agent舆情分析助手,打破信息茧房,还原舆情原貌,预测未来走向,辅助决策!从0实现,不依赖任何框架。

PythonPyPIGNU General Public License v2.0agent-frameworkdata-analysis
deepwiki.com/666ghj/BettaFish
41.8k7.6k
mouredev/Hello-Python

Curso para aprender el lenguaje de programación Python desde cero y para principiantes. 100 clases, 44 horas en vídeo, código, proyectos y grupo de chat. Fundamentos, frontend, backend, testing, IA...

PythonPyPIApache License 2.0fastapimongodb
mouredev.link/python
36.5k2.3k
doocs/leetcode

🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解

JavaMavenCreative Commons Attribution Share Alike 4.0 Internationalalgorithmscpp
leetcode.doocs.org
36.3k9.4k
soxoj/maigret

🕵️‍♂️ Collect a dossier on a person by username from 3000+ sites

PythonPyPIMIT Licenseosintsocial-network
maigret.app
35.6k2.7k
zhayujie/chatgpt-on-wechat

基于大模型搭建的聊天机器人,同时支持 微信公众号、企业微信应用、飞书、钉钉 等接入,可选择GPT3.5/GPT-4o/GPT-o1/ DeepSeek/Claude/文心一言/讯飞星火/通义千问/ Gemini/GLM-4/Claude/Kimi/LinkAI,能处理文本、语音和图片,访问操作系统和互联网,支持基于自有知识库进行定制企业智能客服。

PythonPyPIMIT Licensechatgptwechat
docs.link-ai.tech/cow
35.5k9k
celery/celery

Distributed Task Queue (development branch)

PythonPyPIOtherpythontask-manager
docs.celeryq.dev
28.7k5.1k