-
Notifications
You must be signed in to change notification settings - Fork 0
/
wenku8_list.py
49 lines (37 loc) · 1.06 KB
/
wenku8_list.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
import requests as req
from bs4 import BeautifulSoup as bs
import time
import random
from fake_useragent import UserAgent
ua = UserAgent(cache = True)
my_headers = {
'user-agent':ua.random
}
titles = []
num = 1
Flag = True
break_time = 0
while Flag:
try:
url = f'https://www.wenku8.net/book/{num}.htm'
res = req.get(url, headers = my_headers)
res.encoding = 'gbk'
soup = bs(res.text, 'lxml')
check = soup.select_one('title').get_text()
if check == '出现错误':
num += 1
break_time += 1
time.sleep(random.randint(1,3))
continue
else:
titles.append(f"{num} : {soup.select_one('span > b').get_text()}")
num += 1
break_time = 0
time.sleep(random.randint(1,3))
if time == 10:
break
except Exception:
Flag = False
with open('novel_list.txt', 'w', encoding='utf-8') as f:
for title in titles:
f.write(title + '\n')