-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
63 lines (48 loc) · 1.66 KB
/
parser.py
File metadata and controls
63 lines (48 loc) · 1.66 KB
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
# -*- coding: utf-8 -*-
# !/usr/bin/python3
import asyncio
import aiohttp
'''
解析小说页面
'''
sem = asyncio.Semaphore(10)
class Parser:
# 解析页码
def parse_page_list(self):
print('base parse page list')
return []
# 解析页面中的小说数据
async def async_parse_page(self, url, encoding='utf-8'):
with await sem:
async with aiohttp.request('GET', url) as response:
print(url)
response = await response.text(encoding=encoding, errors='ignore')
self.parse_page(response)
def parse_page(self, response):
pass
# 解析小说主页
async def async_parse_detail(self, url, encoding='utf-8'):
with await sem:
with aiohttp.request('GET', url) as response:
response = await response.text(encoding=encoding, errors='ignore')
self.parse_detail(response)
def parse_detail(self, response):
pass
def start(self, url, mode, encode):
print('start')
loop = asyncio.get_event_loop()
if mode == 0:
page_urls = self.parse_page_list()
total = len(page_urls)
tasks = []
for i in range(total):
page_url = page_urls[i]
tasks.append(self.async_parse_page(page_url, encode))
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
elif mode == 1:
loop.run_until_complete(self.async_parse_page(url, encode))
loop.close()
elif mode == 2:
loop.run_until_complete(self.async_parse_detail(url, encode))
loop.close()