-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
296 lines (239 loc) · 12.3 KB
/
test.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#-*- coding:utf-8 -*-
import json
import os
import requests
from bs4 import BeautifulSoup
import urllib.request
import re
import io
import sys
from urllib.parse import quote
import codecs
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
# 函数功能:得到网易新闻
def get163news():
url = "http://news.163.com/rank/" # 请求网易新闻的URL,获取其text文本
wbdata = requests.get(url).text # 对获取到的文本进行解析
soup = BeautifulSoup(wbdata, 'lxml') # 创建一个beautifulsoup对象
news_titles = soup.select("td a") # 从解析文件中通过select选择器定位指定的元素,返回一个列表
comment = soup.select("td.cBlue") #获取网页内容的步骤对应其它网页相同,不予赘述
# 循环链接列表将获取到的标题、时间、来源、评论、正文写进txt文件
start = 3
i = 30
n = 30
for strat in range(30,500):
for n in range(start, start + 29):
link = news_titles[n].get("href")
try:
neteasedata = urllib.request.urlopen(link).read()
neteasedata2 = neteasedata.decode("gbk", "ignore")
soup = BeautifulSoup(neteasedata2, "html.parser")
content = soup.select('p')
title = soup.select('title')
time = soup.select('div.post_time_source')
author = soup.select('div.post_time_source > a.ne_article_source')
if (len(time) != 0):
fo = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\新闻\网易" + str(i) + ".txt", "w+")
if (len(title) != 0):
fo.writelines(" " + title[0].get_text().strip() + "\n")
fo.writelines("时间:" + time[0].get_text().strip() + "\n")
fo.writelines("评论数: " + comment[i].get_text() + "\n" )
if (len(author) != 0):
fo.writelines(author[0].get_text() + '\n')
# print(title[0].get_text())
# print(time[0].string)
# print(author[0].get_text()
for m in range(2, len(content)):
try:
con = content[m].get_text().strip()
if (len(con) != 0):
fo.writelines("\n" + con)
except Exception as err:
print(err)
m += 1
fo.close()
except Exception as err:
print(err)
i += 1
n += 1
start += 60
n = start
i = start
if(start > 270):
break
# 函数功能:得到腾讯新闻首页所有新闻链接
def getQQurl():
url = "http://news.qq.com/"
wbdata = requests.get(url).text
soup = BeautifulSoup(wbdata, 'lxml')
news_titles = soup.select("div.text > em.f14 > a.linkto")
fo = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\QQ链接.txt", "w+",encoding='utf-8') # 创建TXT文件保存首页所有链接
# 对返回的列表进行遍历
for n in news_titles:
title = n.get_text()
link = n.get("href")
fo.writelines(link + "\n")
fo.close()
# 函数功能:根据获取的链接依次爬取新闻正文并保存到本地
def getqqtext():
qqf = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\QQ链接.txt", "r",encoding='utf-8')
qqurl = qqf.readlines() # 读取文件,得到一个链接列表
i = 0
# 遍历列表,请求网页,筛选出正文信息
for qurl in qqurl:
try:
data = urllib.request.urlopen(qurl).read()
data2 = data.decode("gbk", "ignore")
soup = BeautifulSoup(data2, "html.parser") # 从解析文件中通过select选择器定位指定的元素,返回一个列表
content = soup.select('p') # 选择正文内容
title = soup.select('title') # 选择标题
time = soup.select('div.a_Info > span.a_time')
author = soup.select('div.a_Info > span.a_source')
# 将得到的网页正文写进本地文件
fo = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\新闻\腾讯" + str(i) + ".txt", "w+",encoding='utf-8')
if (len(title) != 0):
fo.writelines(" " + title[0].get_text().strip() + "\n")
if(len(time)!=0):
fo.writelines("时间:"+time[0].get_text().strip() + "\n")
if (len(author) != 0):
fo.writelines("来源:"+author[0].get_text() + '\n'+ "\n")
# print(title[0].get_text())
# print(time[0].string)
# print(author[0].get_text()
for m in range(0, len(content)):
con = content[m].get_text().strip()
if (len(con) != 0):
fo.writelines("\n" + con)
m += 1
fo.close()
except Exception as err:
print(err)
i += 1
#函数功能:得到搜狐新闻首页所有新闻链接
def getsohuurl():
url = "http://news.sohu.com/"
wbdata = requests.get(url).text
soup = BeautifulSoup(wbdata, 'lxml')
news_titles = soup.select("div.list16 > ul > li > a")
fo = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sohu链接.txt", "w+",encoding='utf-8')
for n in news_titles:
title = n.get_text()
link = n.get("href")
fo.writelines(link + "\n")
fo.close()
# 函数功能:根据获取的搜狐新闻链接依次爬取新闻正文并保存到本地
def getsohutext():
sohuf = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sohu链接.txt", "r",encoding='utf-8')
sohuurl = sohuf.readlines()
i = 0
for sohuu in sohuurl:
try:
sohudata = urllib.request.urlopen(sohuu).read()
sohudata2 = sohudata.decode("utf-8", "ignore")
soup = BeautifulSoup(sohudata2, "html.parser")
content = soup.select('p')
title = soup.select('title')
time = soup.select('div.article-info > span.time')
author = soup.select('div.date-source > span.original-link')
if (len(time) != 0):
fo = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\新闻\搜狐" + str(i) + ".txt", "w+",encoding='utf-8')
if (len(title) != 0):
fo.writelines( " " + title[0].get_text().strip() + "\n")
fo.writelines("时间:" + time[0].get_text().strip() + "\n")
fo.writelines("评论数: 0" + "\n" + "\n")
if (len(author) != 0):
fo.writelines(author[0].get_text() + '\n')
# print(title[0].get_text())
# print(time[0].string)
# print(author[0].get_text()
for m in range(0, len(content)):
con = content[m].get_text().strip()
if (len(con) != 0):
fo.writelines("\n" + con)
m += 1
fo.close()
except Exception as err:
print(err)
i += 1
#函数功能:得到新浪新闻首页所有新闻链接
def getsinaurl():
url = ['http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=qbpdpl&top_time=20180715&top_show_num=100&top_order=DESC&js_var=comment_all_data',
'http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=www_www_all_suda_suda & top_time=20180715&top_show_num=100&top_order=DESC&js_var=all_1_data01',
'http://top.collection.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=wbrmzf_qz&top_time=20180715&top_show_num=10&top_order=DESC&js_var=wbrmzf_qz_1_data&call_back=showContent',
'http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=total_slide_suda&top_time=20180715&top_show_num=100&top_order=DESC&js_var=slide_image_1_data',
'http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=wbrmzfgwxw&top_time=20180715&top_show_num=10&top_order=DESC&js_var=wbrmzfgwxw_1_data&call_back=showContent',
'http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=news_china_suda&top_time=20180715&top_show_num=20&top_order=DESC&js_var=news_',
'http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=gnxwpl&top_time=20180715&top_show_num=20&top_order=DESC&js_var=news_']
furl = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sina链接1.txt", "w+",encoding='utf-8')
fcom = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sinacom.txt", "w+",encoding='utf-8')
for u in url:
try:
wbdata = requests.get(u).text
fo = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sinau.txt", "w+",encoding='utf-8')
fo.write(wbdata)
fo.close()
text = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sinau.txt", "r",encoding='utf-8').read()
allurl = re.findall('"url":"(.+?)",', text)
topnum = re.findall('"top_num":"(.+?)",', text)
print(len(allurl))
print(len(topnum))
for n in allurl:
# s=n.encode ("utf-8")
# print(s)
furl.writelines(n + "\n")
for n in topnum:
fcom.writelines(n + "\n")
except Exception as err:
print(err)
furl.close()
fcom.close()
# sinaf = codecs.open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news/sina链接1.txt", 'r', 'utf-8')
# 函数功能:根据获取的新浪新闻链接依次爬取新闻正文并保存到本地
def getsinanews():
sinaf1 = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sina链接1.txt", "r",encoding='utf-8')
sinaf2 = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\sinacom.txt", "r",encoding='utf-8')
sinaurl = sinaf1.readlines()
sinacom = sinaf2.readlines()
i = 0
for surl in sinaurl:
try:
realurl = surl.replace('\/', '/')
sinadata = urllib.request.urlopen(realurl).read()
sinadata2 = sinadata.decode("utf-8", "ignore")
soup = BeautifulSoup(sinadata2, "html.parser")
content = soup.select('p')
title = soup.select('title')
time = soup.select('div.date-source > span.date')
author = soup.select('div.date-source > a.source')
# comments = soup.select('div.hd clearfix > span.count > em > a.comment_participatesum_p')
# print(len(comments))
if (len(time) != 0):
fo = open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\新闻\新浪" + str(i) + ".txt", "w+",encoding='utf-8')
if (len(title) != 0):
fo.writelines(" " + title[0].get_text().strip() + "\n")
fo.writelines("时间:" + time[0].get_text().strip() + "\n")
fo.writelines("评论数: " + sinacom[i] )
if (len(author) != 0):
fo.writelines(author[0].get_text() + '\n')
for m in range(0, len(content)):
con = content[m].get_text().strip()
if (len(con) != 0):
fo.writelines("\n" + con)
m += 1
fo.close()
except Exception as err:
print(err)
i += 1
# def main():
# # get163news()
# getQQurl()
# getqqtext()
# getsinaurl()
# getsinanews()
# getsohuurl()
# getsohutext()
#
# main()
f=open(r"C:\Users\黄寄\Desktop\信息采集与语义分析系统\static\news\新闻\新浪1.txt", "r",encoding='utf-8')
text=f.read()
print(text)