-
Notifications
You must be signed in to change notification settings - Fork 0
/
xinhuanet.py
218 lines (189 loc) · 6.71 KB
/
xinhuanet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
#coding: utf-8
''' 抓取 www.xinhuanet.com 新闻
'''
# url = "http://www.sd.xinhuanet.com/news/2019-10/03/c_1125069429.htm"
_baseurl = "http://www.xinhuanet.com"
import requests
from bs4 import BeautifulSoup as bs
import re
from db import DB
def get(url):
sess = requests.session()
# to simulite browser
sess.headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36"
res = sess.get(url)
return res
_sites = [
{
"title": "主页",
"url": "http://www.xinhuanet.com",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "时政",
"url": "http://www.xinhuanet.com/politics/",
# http://www.xinhuanet.com/politics/2019-10/03/c_1125070507.htm
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "地方",
"url": "http://www.xinhuanet.com/local/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "法制",
"url": "http://www.xinhuanet.com/legal/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "高层",
"url": "http://www.xinhuanet.com/politics/leaders/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "人事",
"url": "http://www.xinhuanet.com/renshi/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "理论",
"url": "http://www.xinhuanet.com/politics/xhll.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "国际",
"url": "http://www.xinhuanet.com/world/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "军事",
"url": "http://www.xinhuanet.com/mil/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "访谈",
"url": "http://www.xinhuanet.com/video/xhft.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "港澳",
"url": "http://www.xinhuanet.com/gangao/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "台湾",
"url": "http://www.xinhuanet.com/tw/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "广播",
"url": "http://www.xinhuanet.com/video/xinhuaradio/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "财经",
"url": "http://www.xinhuanet.com/fortune",
# http://www.xinhuanet.com/fortune/2019-10/05/c_1125072611.htm
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "教育",
"url": "http://education.news.cn/",
#http://education.news.cn/2019-09/29/c_1210296480.htm
"pattern": r"https?://education\.news\.cn/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "科技",
"url": "http://www.xinhuanet.com/tech/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "能源",
"url": "http://www.xinhuanet.com/energy/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "一带一路",
"url": "http://www.xinhuanet.com/silkroad/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "文化",
"url": "http://www.xinhuanet.com/culture/",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "数据",
"url": "http://www.xinhuanet.com/datanews/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "能源",
"url": "http://www.xinhuanet.com/energy/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "信息化",
"url": "http://www.xinhuanet.com/info/index.htm",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "健康",
"url": "http://www.xinhuanet.com/health",
"pattern": r"https?://www\.([^\.]+\.)?xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
{
"title": "体育",
"url": "http://www.xinhuanet.com/sports",
# http://sports.xinhuanet.com/c/2019-10/03/c_1125069344.htm
"pattern": r"https?://sports\.xinhuanet\.com/[^/]+/\d+-\d+/\d+/[^\.]+\.html?",
},
]
def get_main_links(logger, site):
urls = []
res = get(site["url"])
ct = res.content.decode(res.apparent_encoding)
p = re.compile(site["pattern"])
m = p.search(ct)
while m:
urls.append(m.group(0))
ct = ct[m.span()[1]:]
m = p.search(ct)
return urls
def get_page_content(url):
res = get(url)
soup = bs(res.content, features="html.parser")
div_id_names = [
"p-detail",
"contentMain",
]
div_class_names = [
"container",
"article",
]
for idname in div_id_names:
t = soup.find("div", attrs={"id": idname})
if t:
ps = t.find_all("p")
return [p.text for p in ps]
for clsname in div_class_names:
t = soup.find("div", attrs={"class": clsname})
if t:
ps = t.find_all("p")
return [p.text for p in ps]
return []
def grab(logger):
db = DB()
for site in _sites:
urls = get_main_links(logger, site)
logger.info("grab {} urls from {}".format(len(urls), site["title"]))
for url in urls:
if db.has(url):
continue
txt_list = get_page_content(url)
txt = ""
if txt_list:
txt = "\n".join(txt_list)
db.save(url, txt)
logger.info("grab {} got {} bytes".format(url, len(txt)))