-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathGet_novel.py
189 lines (155 loc) · 6.61 KB
/
Get_novel.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
import sys
import re
import random
import datetime
from pymongo import *
from urllib.request import urlopen
from urllib.parse import urlparse
from urllib.error import HTTPError
from bs4 import BeautifulSoup
#================================================(def)
def HTTP_lift(url):
try:
html = urlopen(url)
except:
print("is ERROR")
follow_External_Only("http://syosetu.com/")
k = 0
return None
return "OK"
#------------------------------------------------
def get_Internal_Link(bs4, includeUrl):
internal_Link = []
for link in bs4.findAll("a", href=re.compile("^(/|.*" + includeUrl + ")")):
if link.attrs['href'] not in internal_Link:
internal_Link.append(link.attrs['href'])
return internal_Link
#--------------------------------------------------
def get_External_Link(bs4, excludeUrl):
external_Link = []
for link in bs4.findAll("a", href=re.compile("^(http://.*" + excludeUrl + ").*")):
if link.attrs['href'] is not None:
if link.attrs['href'] not in external_Link:
external_Link.append(link.attrs['href'])
return external_Link
#--------------------------------------------------
def split_Address(address):
address_part = address.replace("http://", "").split("/")
return address_part
#--------------------------------------------------
def get_Random_External_Link(startingPage):
global k
if HTTP_lift(startingPage) == None:
return None
else:
html = urlopen(startingPage)
bs4 = BeautifulSoup(html, "lxml")
external_Link = get_External_Link(bs4, urlparse(startingPage).netloc)
if len(external_Link) == 0:
# print("\t\t\t\t\t\t\t---------error number is : " +
# str(k) + "---------------------")
# k += 1
# print("\t\t\t\t\t\t\tNo external link")
domain = urlparse(startingPage).scheme + "://" + \
urlparse(startingPage).netloc
internal_Link = get_Internal_Link(bs4, domain)
try:
return get_Random_External_Link(internal_Link[random.randint(0, len(internal_Link) - 1)])
except ValueError:
return "http://syosetu.com/"
else:
return external_Link[random.randint(0, len(external_Link) - 1)]
#---------------------------------------------------
def follow_External_Only(startingSite):
external_Link = get_Random_External_Link(startingSite)
if HTTP_lift(external_Link) != None:
display(external_Link)
follow_External_Only(external_Link)
#--------------------------------------------------
def display(external_Link):
global i
global k
if external_Link != "http://syosetu.com/":
html = urlopen(external_Link)
bs4 = BeautifulSoup(html, "lxml")
if len(bs4.findAll("div", {"id": "novel_ex"})) != 0:
print('===============find number is : ' +
str(i) + '====================')
try:
print("Random external Link is :" + external_Link)
except TypeError:
print("THE LINK TYPE ERROR")
try:
if HTTP_lift("http://ncode.syosetu.com/novelview/infotop/ncode" + urlparse(external_Link).path) != None:
html = urlopen(
"http://ncode.syosetu.com/novelview/infotop/ncode" + urlparse(external_Link).path)
bs4 = BeautifulSoup(html, "lxml")
liste = []
if bs4.find("p", {"id": "ncode"}).get_text() not in nolist:
nolist.add(bs4.find("p", {"id": "ncode"}).get_text())
liste.append(bs4.h1.get_text()) # 標題
for temp in bs4.findAll("p", {"id": "ncode"}):
liste.append(temp.get_text(":", strip=True))
if len(bs4.findAll("span", {"id": "noveltype_notend"})) == 0:
for temp in bs4.findAll("span", {"id": "noveltype"}):
liste.append(temp.get_text(":", strip=True))
else:
for temp in bs4.findAll("span", {"id": "noveltype_notend"}):
liste.append(temp.get_text(":", strip=True))
for temp in bs4.find("table", {"id": "noveltable1"}).findAll("tr", limit=3):
liste.append(temp.get_text("\n", strip=True))
for temp in bs4.find("table", {"id": "noveltable2"}).findAll("tr", limit=2):
liste.append(temp.get_text(":", strip=True))
liste.append(bs4.find("table", {"id": "noveltable2"}).find(
text="文字数").parent.parent.get_text(":", strip=True))
importdata = {
"link": external_Link,
"title": liste[0],
"NO_ID": liste[1],
"id_end": liste[2],
"Introduction": liste[3],
"author": liste[4],
"Keywords": liste[5],
"start_date": liste[6],
"end_date": liste[7],
"Word_Count": liste[8]
}
collection.insert(importdata)
k = 0
print(importdata)
print("\n\n\n\n\n\n")
print(nolist)
liste = []
k += 1
else:
k += 1
except AttributeError:
print("NOT TITLE OR novel")
for temp in liste:
print(temp)
print("\n\n")
i += 1
elif k > 20:
follow_External_Only("http://syosetu.com/")
random.seed(datetime.datetime.now())
print("!!!!!!!!!!!!!!!!!!找不到!!!!!!!!!!!!!")
k = 0
else:
k += 1
print("目前 未找到數為:" + str(k))
#================================================(dim)
i = 1
k = 1
nolist = set()
random.seed(datetime.datetime.now())
sys.setrecursionlimit(1000000)
#================================================(main)
client = MongoClient()
client = MongoClient("localhost", 27017)
db = client.novel
collection = db.novel
for temp in collection.find({}, {"NO_ID": 1, "_id": 0}):
for Key, value in temp.items():
nolist.add(value)
print(nolist)
follow_External_Only("http://syosetu.com/")