Skip to content

Commit e4bdccf

Browse files
authored
Merge pull request GeneralNewsExtractor#86 from kingname/develop
自动提取新闻列表页
2 parents d68e894 + 0d359ca commit e4bdccf

File tree

16 files changed

+20836
-245
lines changed

16 files changed

+20836
-245
lines changed

CHANGELOG.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# General News Extractor Changelog
2+
3+
## 0.2.2 (2020-08-02)
4+
5+
### Features
6+
7+
1. 指定列表页特征,自动提取列表页数据
8+
9+
10+
## 0.2.1 (2020-06-27)
11+
12+
### Feature
13+
14+
1. 不再需要计算文本密度的标准差
15+
2. 🚀减少重复计算,大幅度提升分析速度
16+
17+
## 0.2.0 (2020-06-06)
18+
19+
### Feature
20+
21+
1. 优化标题提取逻辑,根据@止水 和 @asyncins 的建议,通过对比 //title/text()中的文本与 <h> 标签中的文本,提取出标题。
22+
2. 增加 `body_xpath`参数,精确定义正文所在的位置,强力避免干扰。
23+
24+
例如对于澎湃新闻,在不设置`body_xpath`参数时:
25+
26+
```python
27+
result = extractor.extract(html,
28+
host='https://www.xxx.com',
29+
noise_node_list=['//div[@class="comment-list"]',
30+
'//*[@style="display:none"]',
31+
'//div[@class="statement"]'
32+
])
33+
```
34+
35+
提取效果如下:
36+
37+
![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2020-06-06-11-51-44.png)
38+
39+
设置了`body_xpath`以后:
40+
41+
```python
42+
result = extractor.extract(html,
43+
host='https://www.xxx.com',
44+
body_xpath='//div[@class="news_txt"]', # 缩小正文提取范围
45+
noise_node_list=['//div[@class="comment-list"]',
46+
'//*[@style="display:none"]',
47+
'//div[@class="statement"]'
48+
])
49+
```
50+
51+
结果如下:
52+
53+
![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2020-06-06-11-53-30.png)
54+
55+
56+
## 0.1.9 (2020-03-11)
57+
58+
### Bug fix
59+
60+
1. 预处理可能会破坏 HTML 结构,导致用户自定义的 XPath 无法正确工作,因此需要把提取用户名、发布时间、标题的代码放在预处理之前。
61+
62+
## 0.1.8 (2020-02-21)
63+
64+
1. 感谢@止水提供的 meta 对应的新闻时间属性,现在会从 HTML 的 meta 数据中检查是否有发布时间。
65+
66+
## 0.1.7 (2020-02-13)
67+
68+
1. 在GeneralNewsExtractor().extract()方法中传入参数`author_xpath``publish_time_xpath`强行指定抓取作者与发布时间的位置。
69+
2. 在.gne 配置文件中,通过如下两个配置分别指定作者与发布时间的 XPath
70+
71+
```yaml
72+
author:
73+
xpath: //meta[@name="author"]/@content
74+
publish_time:
75+
xpath: //em[@id="publish_time"]/text()
76+
```
77+
78+
## 0.1.6 (2020-01-04)
79+
80+
1. 修复由于`node.getparent().remove()`会移除父标签中,位于自己后面的 text 的问题
81+
2. 对于class 中含有`article`/`content`/`news_txt`/`post_text`的标签,增加权重
82+
3. 使用更科学的方法移除无效标签
83+
84+
## 0.1.5 (2019-12-31)
85+
86+
通用参数可以通过 YAML、JSON 批量设置了。只需要在项目的根目录下创建一个 ``.gne`` ,就可以实现函数默认参数的功能。
87+
88+
## 0.1.4 (2019-12-29)
89+
90+
1. 现在可以通过传入参数`host`来把提取的图片url 拼接为绝对路径
91+
92+
例如:
93+
94+
```python
95+
extractor = GeneralNewsExtractor()
96+
result = extractor.extract(html,
97+
host='https://www.xxx.com')
98+
```
99+
100+
返回数据中:
101+
102+
```python
103+
{
104+
...
105+
"images": [
106+
"https://www.xxx.com/W020190918234243033577.jpg"
107+
]
108+
}
109+
```
110+
111+
## 0.1.3 (2019-11-24)
112+
113+
1. 增加更多的 UselessAttr
114+
2. 返回的结果包含`images`字段,里面的结果是一个列表,保存了正文中的所有图片 URL
115+
3. 指定`with_body_html`参数,返回的数据中将会包含`body_html`字段,这是正文的 HTMl 源代码:
116+
117+
```python
118+
...
119+
result = GeneralNewsExtractor().extract(html, with_body_html=True)
120+
body_html = result['body_html']
121+
print(f'正文的网页源代码为:{body_html}')
122+
```

README.md

Lines changed: 30 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
如果你想体验 GNE 的功能,请按照如下步骤进行:
2828

29-
1. 安装 GNE
29+
#### 安装 GNE
3030

3131
```bash
3232

@@ -40,7 +40,9 @@ pipenv install gne
4040

4141
```
4242

43-
2. 使用 GNE
43+
#### 使用 GNE
44+
45+
##### 提取正文
4446

4547
```python
4648
>>> from gne import GeneralNewsExtractor
@@ -56,6 +58,16 @@ pipenv install gne
5658

5759
更多使用说明,请参阅 [GNE 的文档](https://generalnewsextractor.readthedocs.io/)
5860

61+
##### 提取列表页(测试版)
62+
63+
```python
64+
>>> from gne import ListPageExtractor
65+
>>> html = '''经过渲染的网页 HTML 代码'''
66+
>>> list_extractor = ListPageExtractor()
67+
>>> result = list_extractor.extract(html,
68+
feature='列表中任意元素的 XPath")
69+
>>> print(result)
70+
```
5971

6072
### 开发环境
6173

@@ -123,7 +135,13 @@ print(result)
123135
result = extractor.extract(html, noise_node_list=['//div[@class="comment-list"]'])
124136
```
125137

126-
`test`文件夹中的网页的提取结果,请查看`result.txt`
138+
* **提取新闻列表页的功能是测试功能,请勿用于生产环境**。你可以通过Chrome 浏览器开发者工具中的 `Copy XPath` 来复制列表中任意一项的XPath,如下图所示。
139+
140+
![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/2020-08-02-17-07-19.png)
141+
142+
GNE 会根据这一项的 XPath,自动找到这个列表里面其他行的数据。
143+
144+
127145

128146
## 运行截图
129147

@@ -143,6 +161,12 @@ result = extractor.extract(html, noise_node_list=['//div[@class="comment-list"]'
143161

144162
![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/WX20191126-004218.png)
145163

164+
### 网易新闻首页列表
165+
166+
![](https://github.com/kingname/GeneralNewsExtractor/blob/master/screenshots/WX20200802-170137@2x.png)
167+
168+
169+
146170
## 项目文档
147171

148172
[GNE 常见问题 Q&A](https://github.com/kingname/GeneralNewsExtractor/wiki/GeneralNewsExtractor-Q&A)
@@ -152,124 +176,15 @@ result = extractor.extract(html, noise_node_list=['//div[@class="comment-list"]'
152176
1. 目前本项目只适用于新闻页的信息提取。如果目标网站不是新闻页,或者是今日头条中的相册型文章,那么抽取结果可能不符合预期。
153177
2. 可能会有一些新闻页面出现抽取结果中的作者为空字符串的情况,这可能是由于文章本身没有作者,或者使用了已有正则表达式没有覆盖到的情况。
154178

155-
## Changelog
156-
157-
### 2020.06.27
158-
159-
1. 不再需要计算文本密度的标准差
160-
2. 🚀减少重复计算,大幅度提升分析速度
161-
162-
### 2020.06.06
163-
164-
1. 优化标题提取逻辑,根据@止水 和 @asyncins 的建议,通过对比 //title/text()中的文本与 <h> 标签中的文本,提取出标题。
165-
2. 增加 `body_xpath`参数,精确定义正文所在的位置,强力避免干扰。
166-
167-
例如对于澎湃新闻,在不设置`body_xpath`参数时:
168-
169-
```python
170-
result = extractor.extract(html,
171-
host='https://www.xxx.com',
172-
noise_node_list=['//div[@class="comment-list"]',
173-
'//*[@style="display:none"]',
174-
'//div[@class="statement"]'
175-
])
176-
```
177-
178-
提取效果如下:
179-
180-
![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2020-06-06-11-51-44.png)
181-
182-
设置了`body_xpath`以后:
183-
184-
```python
185-
result = extractor.extract(html,
186-
host='https://www.xxx.com',
187-
body_xpath='//div[@class="news_txt"]', # 缩小正文提取范围
188-
noise_node_list=['//div[@class="comment-list"]',
189-
'//*[@style="display:none"]',
190-
'//div[@class="statement"]'
191-
])
192-
```
193-
194-
结果如下:
195-
196-
![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2020-06-06-11-53-30.png)
197-
198-
199-
### 2020.03.11
200-
201-
1. 预处理可能会破坏 HTML 结构,导致用户自定义的 XPath 无法正确工作,因此需要把提取用户名、发布时间、标题的代码放在预处理之前。
202-
203-
### 2020.02.21
204-
205-
1. 感谢@止水提供的 meta 对应的新闻时间属性,现在会从 HTML 的 meta 数据中检查是否有发布时间。
206-
207-
### 2020.02.13
208-
209-
1. 在GeneralNewsExtractor().extract()方法中传入参数`author_xpath``publish_time_xpath`强行指定抓取作者与发布时间的位置。
210-
2. 在.gne 配置文件中,通过如下两个配置分别指定作者与发布时间的 XPath
211-
212-
```yaml
213-
author:
214-
xpath: //meta[@name="author"]/@content
215-
publish_time:
216-
xpath: //em[@id="publish_time"]/text()
217-
```
218-
219-
### 2020.01.04
220-
221-
1. 修复由于`node.getparent().remove()`会移除父标签中,位于自己后面的 text 的问题
222-
2. 对于class 中含有`article`/`content`/`news_txt`/`post_text`的标签,增加权重
223-
3. 使用更科学的方法移除无效标签
224-
225-
### 2019.12.31
226-
227-
通用参数可以通过 YAML、JSON 批量设置了。只需要在项目的根目录下创建一个 ``.gne`` ,就可以实现函数默认参数的功能。
228-
229-
### 2019.12.29
230-
231-
1. 现在可以通过传入参数`host`来把提取的图片url 拼接为绝对路径
232-
233-
例如:
234-
235-
```python
236-
extractor = GeneralNewsExtractor()
237-
result = extractor.extract(html,
238-
host='https://www.xxx.com')
239-
```
240-
241-
返回数据中:
242-
243-
```python
244-
{
245-
...
246-
"images": [
247-
"https://www.xxx.com/W020190918234243033577.jpg"
248-
]
249-
}
250-
```
251-
252-
### 2019.11.24
253-
254-
1. 增加更多的 UselessAttr
255-
2. 返回的结果包含`images`字段,里面的结果是一个列表,保存了正文中的所有图片 URL
256-
3. 指定`with_body_html`参数,返回的数据中将会包含`body_html`字段,这是正文的 HTMl 源代码:
257-
258-
```python
259-
...
260-
result = GeneralNewsExtractor().extract(html, with_body_html=True)
261-
body_html = result['body_html']
262-
print(f'正文的网页源代码为:{body_html}')
263-
```
264179

265180
## Todo
266181

267182
* ~~使用一个配置文件来存放常量数据,而不是直接 Hard Code 写在代码中。~~
268183
* ~~允许自定义时间、作者的提取Pattern~~
269-
* 新闻文章列表页提取
184+
* ~~新闻文章列表页提取~~
270185
* 对于多页的新闻,允许传入一个 HTML 列表,GNE 解析以后,自动拼接为完整的新闻正文
271-
* 优化内容提取速度
272-
* 测试更多新闻网站
186+
* ~~优化内容提取速度~~
187+
* ~~测试更多新闻网站~~
273188
* ……
274189

275190
## 交流沟通
1.56 MB
Loading
1.71 MB
Loading

0 commit comments

Comments
 (0)