{"id":15032191,"url":"https://github.com/generalnewsextractor/generalnewsextractor","last_synced_at":"2025-05-14T04:07:27.141Z","repository":{"id":37345560,"uuid":"207119273","full_name":"GeneralNewsExtractor/GeneralNewsExtractor","owner":"GeneralNewsExtractor","description":" 新闻网页正文通用抽取器 Beta 版.","archived":false,"fork":false,"pushed_at":"2024-06-25T04:13:11.000Z","size":18268,"stargazers_count":3715,"open_issues_count":28,"forks_count":538,"subscribers_count":85,"default_branch":"master","last_synced_at":"2025-04-19T01:08:24.848Z","etag":null,"topics":["python3","webcrawler","webspider"],"latest_commit_sha":null,"homepage":"https://gnepro.kingname.info/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GeneralNewsExtractor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-08T13:47:38.000Z","updated_at":"2025-04-14T08:42:05.000Z","dependencies_parsed_at":"2023-07-26T11:24:14.963Z","dependency_job_id":"a3f1aa74-ce46-4797-b1a7-61a23e6d879c","html_url":"https://github.com/GeneralNewsExtractor/GeneralNewsExtractor","commit_stats":{"total_commits":104,"total_committers":7,"mean_commits":"14.857142857142858","dds":"0.21153846153846156","last_synced_commit":"64136274a0043ac7056744a438b27190ce7ca0c1"},"previous_names":["kingname/generalnewsextractor"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GeneralNewsExtractor%2FGeneralNewsExtractor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GeneralNewsExtractor%2FGeneralNewsExtractor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GeneralNewsExtractor%2FGeneralNewsExtractor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GeneralNewsExtractor%2FGeneralNewsExtractor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GeneralNewsExtractor","download_url":"https://codeload.github.com/GeneralNewsExtractor/GeneralNewsExtractor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254069476,"owners_count":22009557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python3","webcrawler","webspider"],"created_at":"2024-09-24T20:17:42.710Z","updated_at":"2025-05-14T04:07:27.042Z","avatar_url":"https://github.com/GeneralNewsExtractor.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GNE\n\n![](https://github.com/GeneralNewsExtractor/GeneralNewsExtractor/raw/master/screenshots/logo.png)\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=GeneralNewsExtractor/GeneralNewsExtractor\u0026type=Date)](https://star-history.com/#GeneralNewsExtractor/GeneralNewsExtractor\u0026Date)\n\n\n## 项目起源\n\n开发这个项目，源自于我在知网发现了一篇关于自动化抽取新闻类网站正文的算法论文——[《基于文本及符号密度的网页正文提取方法》](https://kns.cnki.net/KCMS/detail/detail.aspx?dbcode=CJFQ\u0026dbname=CJFDLAST2019\u0026filename=GWDZ201908029\u0026v=MDY4MTRxVHJXTTFGckNVUkxPZmJ1Wm5GQ2poVXJyQklqclBkTEc0SDlqTXA0OUhiWVI4ZVgxTHV4WVM3RGgxVDM=)）\n\n这篇论文中描述的算法看起来简洁清晰，并且符合逻辑。但由于论文中只讲了算法原理，并没有具体的语言实现，所以我使用 Python 根据论文实现了这个抽取器。并分别使用今日头条、网易新闻、游民星空、观察者网、凤凰网、腾讯新闻、ReadHub、新浪新闻做了测试，发现提取效果非常出色，几乎能够达到100%的准确率。\n\n## 项目现状\n\n在论文中描述的正文提取基础上，我增加了标题、发布时间和文章作者的自动化探测与提取功能。\n\n目前这个项目是一个非常非常早期的 Demo，不能保证每个版本向下兼容，升级之前请特别注意。发布出来是希望能够尽快得到大家的使用反馈，从而能够更好地有针对性地进行开发。\n\n本项目取名为`抽取器`，而不是`爬虫`，是为了规避不必要的风险，因此，本项目的输入是 HTML，输出是一个字典。请自行使用恰当的方法获取目标网站的 HTML。\n\n**本项目现在不会，将来也不会提供主动请求网站 HTML 的功能。**\n\n## 如何使用\n\n### 在线体验\n\n如果你想先体验 GNE 的提取效果，那么你可以访问[http://gne.kingname.info/](http://gne.kingname.info/)。\n一般情况下，你只需要把网页粘贴到最上面的多行文本框中，然后点`提取`按钮即可。通过附加更多的参数，可以让提取更精确。具体\n参数的写法与作用，请参阅 [API](https://generalnewsextractor.readthedocs.io/zh_CN/latest/#api)\n\n### 使用环境\n\n如果你想体验 GNE 的功能，请按照如下步骤进行：\n\n#### 安装 GNE\n\n```bash\n\n# 以下两种方案任选一种即可\n\n# 使用 pip 安装\npip install --upgrade gne\n\n# 使用 pipenv 安装\npipenv install gne\n\n```\n\n#### 使用 GNE\n\n##### 提取正文\n\n```python\n\u003e\u003e\u003e from gne import GeneralNewsExtractor\n\n\u003e\u003e\u003e html = '''经过渲染的网页 HTML 代码'''\n\n\u003e\u003e\u003e extractor = GeneralNewsExtractor()\n\u003e\u003e\u003e result = extractor.extract(html, noise_node_list=['//div[@class=\"comment-list\"]'])\n\u003e\u003e\u003e print(result)\n\n{\"title\": \"xxxx\", \"publish_time\": \"2019-09-10 11:12:13\", \"author\": \"yyy\", \"content\": \"zzzz\", \"images\": [\"/xxx.jpg\", \"/yyy.png\"]}\n```\n\n更多使用说明，请参阅 [GNE 的文档](https://generalnewsextractor.readthedocs.io/)\n\n##### 提取列表页(测试版)\n    \n```python\n\u003e\u003e\u003e from gne import ListPageExtractor\n\u003e\u003e\u003e html = '''经过渲染的网页 HTML 代码'''\n\u003e\u003e\u003e list_extractor = ListPageExtractor()\n\u003e\u003e\u003e result = list_extractor.extract(html,\n                                    feature='列表中任意元素的 XPath\")\n\u003e\u003e\u003e print(result)\n```\n\n### 开发环境\n\n如果你需要参与本项目的开发，请按照如下步骤进行。\n\n如果你只想阅读源代码，请现在直接**按下键盘上的句号键（就是问号左边那个）**，可以获得更好的阅读体验。\n\n本项目使用 `Pipenv`管理 Python 的第三方库。如果你不知道 `Pipenv` 是什么，请[点我跳转](https://github.com/pypa/pipenv)。\n\n安装完成`Pipenv`以后，按照如下步骤运行代码：\n\n```bash\ngit clone https://github.com/kingname/GeneralNewsExtractor.git\ncd GeneralNewsExtractor\npipenv install\npipenv shell\npython3 example.py\n```\n\n### 特别说明\n\n项目代码中的`example.py`提供了本项目的基本使用示例。\n\n* 本项目的测试代码在`tests`文件夹中\n* 本项目的输入 HTML 为经过 JavaScript 渲染以后的 HTML，而不是普通的网页源代码。所以无论是后端渲染、Ajax 异步加载都适用于本项目。\n* 如果你要手动测试新的目标网站或者目标新闻，那么你可以在 Chrome 浏览器中打开对应页面，然后开启`开发者工具`，如下图所示：\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/2019-09-08-22-20-33.png)\n\n在`Elements`标签页定位到`\u003chtml\u003e`标签，并右键，选择`Copy`-`Copy OuterHTML`，如下图所示\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/2019-09-08-22-21-49.png)\n\n* 当然，你可以使用 Puppeteer/Pyppeteer、Selenium 或者其他任何方式获取目标页面的`JavaScript渲染后的`源代码。\n\n* 获取到源代码以后，通过如下代码提取信息：\n\n``` python\nfrom gne import GeneralNewsExtractor\n\nextractor = GeneralNewsExtractor()\nhtml = '你的目标网页正文'\nresult = extractor.extract(html)\nprint(result)\n```\n\n* 如果标题自动提取失败了，你可以指定 XPath：\n\n```python\nfrom gne import GeneralNewsExtractor\n\nextractor = GeneralNewsExtractor()\nhtml = '你的目标网页正文'\nresult = extractor.extract(html, title_xpath='//h5/text()')\nprint(result)\n```\n\n对大多数新闻页面而言，以上的写法就能够解决问题了。\n\n但某些新闻网页下面会有评论，评论里面可能存在长篇大论，它们会看起来比真正的新闻正文更像是正文，因此`extractor.extract()`方法还有一个默认参数`noise_node_list`，用于在网页预处理时提前把评论区域整个移除。\n\n`noise_mode_list`的值是一个列表，列表里面的每一个元素都是 XPath，对应了你需要提前移除的，可能会导致干扰的目标标签。\n\n例如，`观察者网`下面的评论区域对应的Xpath 为`//div[@class=\"comment-list\"]`。所以在提取观察者网时，为了防止评论干扰，就可以加上这个参数：\n\n```python\nresult = extractor.extract(html, noise_node_list=['//div[@class=\"comment-list\"]'])\n```\n\n* **提取新闻列表页的功能是测试功能，请勿用于生产环境**。你可以通过Chrome 浏览器开发者工具中的 `Copy XPath` 来复制列表中任意一项的XPath，如下图所示。\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/2020-08-02-17-07-19.png)\n\nGNE 会根据这一项的 XPath，自动找到这个列表里面其他行的数据。\n\n\n\n## 运行截图\n\n### 网易新闻\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/WX20191125-231230.png)\n\n### 今日头条\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/WX20191125-225851.png)\n\n### 新浪新闻\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/WX20191125-231506.png)\n\n### 凤凰网\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/WX20191126-004218.png)\n\n### 网易新闻首页列表\n\n![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/WX20200802-170137@2x.png)\n\n\n\n## 项目文档\n\n[GNE 常见问题 Q\u0026A](https://github.com/kingname/GeneralNewsExtractor/wiki/GeneralNewsExtractor-Q\u0026A)\n\n## 已知问题\n\n1. 目前本项目只适用于新闻页的信息提取。如果目标网站不是新闻页，或者是今日头条中的相册型文章，那么抽取结果可能不符合预期。\n2. 可能会有一些新闻页面出现抽取结果中的作者为空字符串的情况，这可能是由于文章本身没有作者，或者使用了已有正则表达式没有覆盖到的情况。\n\n\n## Todo\n\n* ~~使用一个配置文件来存放常量数据，而不是直接 Hard Code 写在代码中。~~\n* ~~允许自定义时间、作者的提取Pattern~~\n* ~~新闻文章列表页提取~~\n* 对于多页的新闻，允许传入一个 HTML 列表，GNE 解析以后，自动拼接为完整的新闻正文\n* ~~优化内容提取速度~~\n* ~~测试更多新闻网站~~\n* ……\n\n## 交流沟通\n\n如果您觉得GNE对您的日常开发或公司有帮助，请加作者微信 mekingname（或扫描下方二维码） 并注明\"GNE\"，作者会将你拉入群。\n\n![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/IMG_3729_2.JPG)\n\n验证消息：`GNE`\n\n如果你不用微信，那么可以加入 Telegram 群：[https://t.me/joinchat/Bc5swww_XnVR7pEtDUl1vw](https://t.me/joinchat/Bc5swww_XnVR7pEtDUl1vw)\n\n\n## 论文修订\n\n在使用 Python 实现这个抽取器的过程中，我发现论文里面的公式和方法存在一些纰漏，会导致部分节点报错。我将会单独写几篇文章来介绍这里的修改。请关注我的微信公众号：未闻Code：\n\n![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/wechatplatform.jpg)\n\n## 致谢\n\n@bigbrother666sh\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeneralnewsextractor%2Fgeneralnewsextractor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeneralnewsextractor%2Fgeneralnewsextractor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeneralnewsextractor%2Fgeneralnewsextractor/lists"}